refactor: 增加微信认证

This commit is contained in:
Argo Zhang 2019-09-18 21:28:12 +08:00
parent 01f070415c
commit 847905adc6
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
2 changed files with 55 additions and 1 deletions

View File

@ -60,7 +60,7 @@ namespace Bootstrap.Admin
services.AddSignalR().AddJsonProtocalDefault();
services.AddSignalRExceptionFilterHandler<SignalRHub>((client, ex) => client.SendMessageBody(ex).ConfigureAwait(false));
services.AddResponseCompression();
services.AddBootstrapAdminAuthentication().AddGitee(OAuthHelper.Configure).AddGitHub(OAuthHelper.Configure).AddWeChat(OAuthHelper.Configure);
services.AddBootstrapAdminAuthentication().AddGitee(OAuthHelper.Configure).AddGitHub(OAuthHelper.Configure).AddWeChat(WeChatHelper.Configure);
services.AddSwagger();
services.AddButtonAuthorization(MenuHelper.AuthorizateButtons);
services.AddBootstrapAdminBackgroundTask();

View File

@ -0,0 +1,54 @@
using Longbow.OAuth;
using Longbow.Security.Cryptography;
using Longbow.WeChatAuth;
using Microsoft.AspNetCore.Authentication.OAuth;
using System;
namespace Bootstrap.DataAccess
{
/// <summary>
/// 微信登陆帮助类
/// </summary>
public static class WeChatHelper
{
/// <summary>
/// 微信登陆配置方法
/// </summary>
/// <typeparam name="TOptions"></typeparam>
/// <param name="option"></param>
public static void Configure<TOptions>(TOptions option) where TOptions : LgbOAuthOptions
{
option.Events.OnCreatingTicket = context =>
{
// 生成用户
var user = ParseUser(context);
user.App = option.App;
OAuthHelper.SaveUser(user, option.Roles);
// 记录登陆日志
context.HttpContext.Log(user.DisplayName, true);
return System.Threading.Tasks.Task.CompletedTask;
};
}
/// <summary>
/// 插入 Gitee 授权用户到数据库中
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
private static User ParseUser(OAuthCreatingTicketContext context)
{
var user = context.User.ToObject<WeChatUser>();
return new User()
{
ApprovedBy = "OAuth",
ApprovedTime = DateTime.Now,
DisplayName = user.NickName,
UserName = user.UnionId,
Password = LgbCryptography.GenerateSalt(),
Icon = user.HeadImgUrl,
Description = $"{context.Scheme.Name}"
};
}
}
}