feat: 增加 QQ 登录

This commit is contained in:
Argo-2016 2020-02-17 22:00:29 +08:00
parent ec692efaa3
commit 0d64c69596
7 changed files with 58 additions and 6 deletions

View File

@ -3,6 +3,7 @@ using Bootstrap.DataAccess;
using Bootstrap.Security.Mvc;
using Longbow.GiteeAuth;
using Longbow.GitHubAuth;
using Longbow.TencentAuth;
using Longbow.Web.SMS;
using Longbow.WeChatAuth;
using Microsoft.AspNetCore.Authentication;
@ -207,6 +208,17 @@ namespace Bootstrap.Admin.Controllers
return Challenge(enabled ? GitHubDefaults.AuthenticationScheme : CookieAuthenticationDefaults.AuthenticationScheme);
}
/// <summary>
/// Tencent 认证
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Tencent([FromServices]IConfiguration config)
{
var enabled = config.GetValue($"{nameof(TencentOptions)}:Enabled", false);
return Challenge(enabled ? TencentDefaults.AuthenticationScheme : CookieAuthenticationDefaults.AuthenticationScheme);
}
/// <summary>
/// WeChat 认证
/// </summary>

View File

@ -59,7 +59,7 @@ namespace Bootstrap.Admin
services.AddOnlineUsers();
services.AddSignalR().AddJsonProtocol(op => op.PayloadSerializerOptions.AddDefaultConverters());
services.AddSignalRExceptionFilterHandler<SignalRHub>(async (client, ex) => await client.SendMessageBody(ex).ConfigureAwait(false));
services.AddBootstrapAdminAuthentication(Configuration).AddGitee(OAuthHelper.Configure).AddGitHub(OAuthHelper.Configure);
services.AddBootstrapAdminAuthentication(Configuration).AddGitee(OAuthHelper.Configure).AddGitHub(OAuthHelper.Configure).AddTencent(OAuthHelper.Configure);
services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireBootstrapAdminAuthorizate().Build());
services.AddButtonAuthorization(MenuHelper.AuthorizateButtons);
services.AddBootstrapAdminBackgroundTask();

View File

@ -130,8 +130,8 @@
</a>
</div>
<div class="item">
<a href="#" data-toggle="tooltip" title="QQ-暂未实现">
<img class="item" src="~/images/qq-2.svg" />
<a href="~/Account/Tencent" data-toggle="tooltip" title="使用 QQ 账号登录">
<img class="item" src="~/images/qq.svg" />
</a>
</div>
<div class="item">

View File

@ -103,7 +103,7 @@
},
"WeChatOptions": {
"Enabled": true,
"ClientId": "<AppId>",
"ClientId": "<ClientId>",
"ClientSecret": "<secret>",
"CallbackPath": "/signin-weixin",
"HomePath": "/Admin/Profiles",
@ -111,6 +111,16 @@
"Roles": [ "Administrators" ],
"App": "Demo"
},
"TencentOptions": {
"Enabled": true,
"ClientId": "<ClientId>",
"ClientSecret": "<ClientSecret>",
"CallbackPath": "/signin-tencent",
"HomePath": "/Admin/Profiles",
"Scope": [ "get_user_info" ],
"Roles": [ "Administrators" ],
"App": "Demo"
},
"SMSOptions": {
"CompanyCode": "<CompanyCode>",
"MD5Key": "MD5Key",

View File

@ -99,7 +99,7 @@
},
"WeChatOptions": {
"Enabled": true,
"ClientId": "<AppId>",
"ClientId": "<ClientId>",
"ClientSecret": "<secret>",
"CallbackPath": "/signin-weixin",
"HomePath": "/Admin/Profiles",
@ -107,6 +107,16 @@
"Roles": [ "Default" ],
"App": "Demo"
},
"TencentOptions": {
"Enabled": true,
"ClientId": "<ClientId>",
"ClientSecret": "<ClientSecret>",
"CallbackPath": "/signin-tencent",
"HomePath": "/Admin/Profiles",
"Scope": [ "get_user_info" ],
"Roles": [ "Administrators" ],
"App": "Demo"
},
"SMSOptions": {
"CompanyCode": "<CompanyCode>",
"MD5Key": "MD5Key",

View File

@ -16,6 +16,7 @@
<PackageReference Include="Longbow.PetaPoco" Version="1.0.2" />
<PackageReference Include="Longbow.Security.Cryptography" Version="1.3.0" />
<PackageReference Include="Longbow.Tasks" Version="3.1.0" />
<PackageReference Include="Longbow.TencentAuth" Version="3.1.0" />
<PackageReference Include="Longbow.Web" Version="3.1.1" />
<PackageReference Include="Longbow.WeChatAuth" Version="3.1.1" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="3.1.1" />

View File

@ -1,5 +1,6 @@
using Longbow.OAuth;
using Longbow.Security.Cryptography;
using Longbow.TencentAuth;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Configuration;
@ -61,6 +62,24 @@ namespace Bootstrap.DataAccess
return user as T;
}
private static OAuthUser? ToTencentUser(this System.Text.Json.JsonElement element)
{
var target = element.EnumerateObject();
var ret = target.TryGetValue("ret");
OAuthUser? user = null;
if (ret == "0")
{
user = new OAuthUser
{
Id = target.TryGetValue("Id"),
Login = target.TryGetValue("nickname"),
Name = target.TryGetValue("nickname"),
Avatar_Url = target.TryGetValue("figureurl_qq_2")
};
}
return user;
}
private static string TryGetValue(this System.Text.Json.JsonElement.ObjectEnumerator target, string propertyName)
{
var ret = string.Empty;
@ -76,7 +95,7 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
private static User ParseUser(OAuthCreatingTicketContext context)
{
var user = context.User.ToObject<OAuthUser>();
var user = context.Scheme.DisplayName == TencentDefaults.DisplayName ? context.User.ToTencentUser() : context.User.ToObject<OAuthUser>();
return new User()
{
ApprovedBy = "OAuth",