!70 增加功能:增加支付宝登录

* build: Alipay 使用正式版组件
* Revert "revert: 移除 Alipay 相关代码"
* revert: 移除 Alipay 相关代码
* feat: 增加 Alipay 功能
* fix: 修正微信登录配置文件名称错误
* refactor: 更新支付宝登录按钮提示文字
* feat: 增加支付宝登录
This commit is contained in:
Argo 2020-02-19 12:12:48 +08:00
parent e3bd7cbbfa
commit 97be2c0eff
12 changed files with 61 additions and 132 deletions

View File

@ -1,6 +1,7 @@
using Bootstrap.Admin.Models;
using Bootstrap.DataAccess;
using Bootstrap.Security.Mvc;
using Longbow.AlipayAuth;
using Longbow.GiteeAuth;
using Longbow.GitHubAuth;
using Longbow.TencentAuth;
@ -219,6 +220,17 @@ namespace Bootstrap.Admin.Controllers
return Challenge(enabled ? TencentDefaults.AuthenticationScheme : CookieAuthenticationDefaults.AuthenticationScheme);
}
/// <summary>
/// Alipay 认证
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Alipay([FromServices]IConfiguration config)
{
var enabled = config.GetValue($"{nameof(AlipayOptions)}:Enabled", false);
return Challenge(enabled ? AlipayDefaults.AuthenticationScheme : CookieAuthenticationDefaults.AuthenticationScheme);
}
/// <summary>
/// WeChat 认证
/// </summary>

View File

@ -59,7 +59,11 @@ 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).AddTencent(OAuthHelper.Configure);
services.AddBootstrapAdminAuthentication(Configuration)
.AddGitee(OAuthHelper.Configure)
.AddGitHub(OAuthHelper.Configure)
.AddTencent(OAuthHelper.Configure)
.AddAlipay(OAuthHelper.Configure);
services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireBootstrapAdminAuthorizate().Build());
services.AddButtonAuthorization(MenuHelper.AuthorizateButtons);
services.AddBootstrapAdminBackgroundTask();

View File

@ -135,8 +135,8 @@
</a>
</div>
<div class="item">
<a href="#" data-toggle="tooltip" title="支付宝-暂未实现">
<img class="item" src="~/images/zhifubao-2.svg" />
<a href="~/Account/Alipay" data-toggle="tooltip" title="使用支付宝账号登录">
<img class="item" src="~/images/zhifubao.svg" />
</a>
</div>
</div>

View File

@ -128,6 +128,17 @@
"HomePath": "/Admin/Profiles",
"App": "Demo"
},
"AlipayOptions": {
"Enabled": true,
"ClientId": "<ClientId>",
"ClientSecret": "<ClientSecret>",
"CallbackPath": "/signin-alipay",
"HomePath": "/Admin/Profiles",
"SignKey": "",
"Scope": [ "auth_user" ],
"Roles": [ "Administrators" ],
"App": "Demo"
},
"TencentSMSOptions": {
"AppId": "<TencentAppId>",
"AppKey": "<TencentAppKey>",

View File

@ -117,6 +117,17 @@
"Roles": [ "Administrators" ],
"App": "Demo"
},
"AlipayOptions": {
"Enabled": true,
"ClientId": "<ClientId>",
"ClientSecret": "<ClientSecret>",
"CallbackPath": "/signin-alipay",
"HomePath": "/Admin/Profiles",
"SignKey": "",
"Scope": [ "auth_user" ],
"Roles": [ "Administrators" ],
"App": "Demo"
},
"SMSOptions": {
"CompanyCode": "<CompanyCode>",
"MD5Key": "MD5Key",

View File

@ -6,7 +6,7 @@
<ItemGroup>
<PackageReference Include="Bootstrap.Security" Version="3.1.0" />
<PackageReference Include="MongoDB.Driver" Version="2.10.1" />
<PackageReference Include="MongoDB.Driver" Version="2.10.2" />
</ItemGroup>
<ItemGroup>

View File

@ -8,20 +8,21 @@
<PackageReference Include="Bootstrap.Security.DataAccess" Version="3.1.1" />
<PackageReference Include="Bootstrap.Security.Mvc" Version="3.1.1" />
<PackageReference Include="Longbow" Version="3.1.0" />
<PackageReference Include="Longbow.AlipayAuth" Version="3.1.0" />
<PackageReference Include="Longbow.Cache" Version="3.1.2-beta-02" />
<PackageReference Include="Longbow.Data" Version="3.1.0" />
<PackageReference Include="Longbow.GiteeAuth" Version="3.1.0" />
<PackageReference Include="Longbow.GitHubAuth" Version="3.1.0" />
<PackageReference Include="Longbow.OAuth" Version="3.1.0" />
<PackageReference Include="Longbow.OAuth" Version="3.1.1" />
<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.TencentAuth" Version="3.1.1" />
<PackageReference Include="Longbow.Web" Version="3.1.1" />
<PackageReference Include="Longbow.WeChatAuth" Version="3.1.1" />
<PackageReference Include="Longbow.WeChatAuth" Version="3.1.2" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="3.1.1" />
<PackageReference Include="PetaPoco.Extensions" Version="3.1.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.1" />
</ItemGroup>
</Project>

View File

@ -1,6 +1,8 @@
using Longbow.OAuth;
using Longbow.AlipayAuth;
using Longbow.OAuth;
using Longbow.Security.Cryptography;
using Longbow.TencentAuth;
using Longbow.WeChatAuth;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Configuration;
@ -51,43 +53,6 @@ namespace Bootstrap.DataAccess
};
}
private static T? ToObject<T>(this System.Text.Json.JsonElement element) where T : OAuthUser
{
var user = new OAuthUser();
var target = element.EnumerateObject();
user.Id = target.TryGetValue("Id");
user.Login = target.TryGetValue("Login");
user.Name = target.TryGetValue("Name");
user.Avatar_Url = target.TryGetValue("Avatar_Url");
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;
var property = target.FirstOrDefault(t => t.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase));
ret = property.Value.ToString();
return ret;
}
/// <summary>
/// 插入 Gitee 授权用户到数据库中
/// </summary>
@ -95,7 +60,13 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
private static User ParseUser(OAuthCreatingTicketContext context)
{
var user = context.Scheme.DisplayName == TencentDefaults.DisplayName ? context.User.ToTencentUser() : context.User.ToObject<OAuthUser>();
var user = context.Scheme.DisplayName switch
{
WeChatDefaults.DisplayName => context.User.ToWeChatUser(),
TencentDefaults.DisplayName => context.User.ToTencentUser(),
AlipayDefaults.DisplayName => context.User.ToAlipayUser(),
_ => context.User.ToAuthUser()
};
return new User()
{
ApprovedBy = "OAuth",

View File

@ -1,81 +0,0 @@
using Longbow.OAuth;
using Longbow.Security.Cryptography;
using Longbow.WeChatAuth;
using Microsoft.AspNetCore.Authentication.OAuth;
using System;
using System.Linq;
using System.Text.Json;
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="context"></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}"
};
}
private static T? ToObject<T>(this JsonElement element) where T : WeChatUser
{
var user = new WeChatUser();
var target = element.EnumerateObject();
user.OpenId = target.TryGetValue("OpenId");
user.UnionId = target.TryGetValue("UnionId");
user.NickName = target.TryGetValue("NickName");
user.Privilege = target.TryGetValue("Privilege");
user.Sex = target.TryGetValue("Sex");
user.Province = target.TryGetValue("Province");
user.City = target.TryGetValue("City");
user.Country = target.TryGetValue("Country");
user.HeadImgUrl = target.TryGetValue("HeadImgUrl");
user.Privilege = target.TryGetValue("Privilege");
return user as T;
}
private static string TryGetValue(this JsonElement.ObjectEnumerator target, string propertyName)
{
var ret = string.Empty;
var property = target.FirstOrDefault(t => t.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase));
ret = property.Value.ToString();
return ret;
}
}
}

View File

@ -5,7 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.10.1" />
<PackageReference Include="MongoDB.Driver" Version="2.10.2" />
</ItemGroup>
<ItemGroup>

View File

@ -8,10 +8,10 @@
<PackageReference Include="Bootstrap.Security.DataAccess" Version="3.1.1" />
<PackageReference Include="Bootstrap.Security.Mvc" Version="3.1.1" />
<PackageReference Include="Longbow" Version="3.1.0" />
<PackageReference Include="Longbow.Cache" Version="3.1.2-beta-01" />
<PackageReference Include="Longbow.Cache" Version="3.1.2-beta-02" />
<PackageReference Include="Longbow.Data" Version="3.1.0" />
<PackageReference Include="Longbow.Web" Version="3.1.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.1" />
</ItemGroup>
</Project>

View File

@ -11,7 +11,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.2" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="3.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MySql.Data" Version="8.0.19" />