fix(#I12XNX): 修复 OAuth 认证

#Issue
close #I12XNX
This commit is contained in:
Argo Zhang 2019-10-02 23:05:55 +08:00
parent 3e1b7e94d3
commit 330167447c
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
3 changed files with 54 additions and 1 deletions

View File

@ -1,9 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netcoreapp3.0;netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Bootstrap.Security.DataAccess" Version="2.2.21" />
<PackageReference Include="Longbow" Version="3.0.0-beta1" />

View File

@ -50,6 +50,27 @@ namespace Bootstrap.DataAccess
};
}
#if NETCOREAPP3_0
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 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;
}
#endif
/// <summary>
/// 插入 Gitee 授权用户到数据库中
/// </summary>

View File

@ -3,6 +3,7 @@ using Longbow.Security.Cryptography;
using Longbow.WeChatAuth;
using Microsoft.AspNetCore.Authentication.OAuth;
using System;
using System.Linq;
namespace Bootstrap.DataAccess
{
@ -50,5 +51,32 @@ namespace Bootstrap.DataAccess
Description = $"{context.Scheme.Name}"
};
}
#if NETCOREAPP3_0
private static T ToObject<T>(this System.Text.Json.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 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;
}
#endif
}
}