!36 增加功能:支持微信登录

Merge pull request !36 from Argo/dev-WeChat
This commit is contained in:
Argo 2019-09-28 13:57:04 +08:00 committed by Gitee
commit 96952ca152
4 changed files with 108 additions and 6 deletions

View File

@ -3,6 +3,7 @@ using Bootstrap.DataAccess;
using Longbow.GiteeAuth;
using Longbow.GitHubAuth;
using Longbow.Web;
using Longbow.WeChatAuth;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
@ -199,5 +200,16 @@ namespace Bootstrap.Admin.Controllers
var enabled = config.GetValue($"{nameof(GitHubOptions)}:Enabled", false);
return Challenge(enabled ? GitHubDefaults.AuthenticationScheme : CookieAuthenticationDefaults.AuthenticationScheme);
}
/// <summary>
/// WeChat 认证
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult WeChat([FromServices]IConfiguration config)
{
var enabled = config.GetValue($"{nameof(GitHubOptions)}:Enabled", false);
return Challenge(enabled ? WeChatDefaults.AuthenticationScheme : CookieAuthenticationDefaults.AuthenticationScheme);
}
}
}

View File

@ -66,8 +66,13 @@
"ClientSecret": "3427f2d901ba9afc76c1842a7303b2d67f8e098e71acc15051f89fe6f3d265db",
"CallbackPath": "/signin-gitee",
"HomePath": "/Admin/Profiles",
"Scope": [ "user_info", "projects" ],
"Roles": [ "Administrators" ],
"Scope": [
"user_info",
"projects"
],
"Roles": [
"Administrators"
],
"App": "0",
"StarredUrl": "https://gitee.com/api/v5/user/starred/LongbowEnterprise/BootstrapAdmin"
},
@ -77,19 +82,49 @@
"ClientSecret": "ffa759ca599df941b869efecb5e750bc1b27334e",
"CallbackPath": "/signin-github",
"HomePath": "/Admin/Profiles",
"Scope": [ "user_info", "repo" ],
"Roles": [ "Administrators" ],
"Scope": [
"user_info",
"repo"
],
"Roles": [
"Administrators"
],
"App": "0",
"StarredUrl": "https://api.github.com/user/starred/ArgoZhang/BootstrapAdmin"
},
"WeChatOptions": {
"Enabled": true,
"ClientId": "<AppId>",
"ClientSecret": "<secret>",
"CallbackPath": "/signin-weixin",
"HomePath": "/Admin/Profiles",
"Scope": [
"snsapi_login"
],
"Roles": [
"Administrators"
],
"App": "0"
},
"SMSOptions": {
"CompanyCode": "<CompanyCode>",
"MD5Key": "MD5Key",
"Roles": [ "Administrators" ],
"Roles": [
"Administrators"
],
"HomePath": "/Admin/Profiles",
"App": "0"
},
"AppMenus": [ "首页", "测试页面", "关于", "返回码云", "多级菜单", "第二层", "第三层", "第四层" ],
"AppMenus": [
"首页",
"测试页面",
"关于",
"返回码云",
"多级菜单",
"第二层",
"第三层",
"第四层"
],
"LongbowCache": {
"Enabled": true,
"CorsItems": [

View File

@ -16,6 +16,7 @@
<PackageReference Include="Longbow.Security.Cryptography" Version="1.3.0" />
<PackageReference Include="Longbow.Tasks" Version="2.2.23" />
<PackageReference Include="Longbow.Web" Version="2.2.16" />
<PackageReference Include="Longbow.WeChatAuth" Version="2.2.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="2.2.6" />
<PackageReference Include="PetaPoco.Extensions" Version="1.0.9" />
</ItemGroup>

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}"
};
}
}
}