From 330167447cab8f795c0dedcccd4017c396fd8af8 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Wed, 2 Oct 2019 23:05:55 +0800 Subject: [PATCH] =?UTF-8?q?fix(#I12XNX):=20=E4=BF=AE=E5=A4=8D=20OAuth=20?= =?UTF-8?q?=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #Issue close #I12XNX --- .../Bootstrap.DataAccess.csproj | 6 +++- .../Helper/OAuthHelper.cs | 21 ++++++++++++++ .../Helper/WeChatHelper.cs | 28 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj b/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj index 13a7775b..8ab799cf 100644 --- a/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj +++ b/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj @@ -1,9 +1,13 @@  - netstandard2.0 + netcoreapp3.0;netstandard2.0 + + + + diff --git a/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs b/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs index ce1e8608..0b143b83 100644 --- a/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs +++ b/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs @@ -50,6 +50,27 @@ namespace Bootstrap.DataAccess }; } +#if NETCOREAPP3_0 + private static T ToObject(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 + /// /// 插入 Gitee 授权用户到数据库中 /// diff --git a/src/admin/Bootstrap.DataAccess/Helper/WeChatHelper.cs b/src/admin/Bootstrap.DataAccess/Helper/WeChatHelper.cs index 491a4f63..eeb5b89a 100644 --- a/src/admin/Bootstrap.DataAccess/Helper/WeChatHelper.cs +++ b/src/admin/Bootstrap.DataAccess/Helper/WeChatHelper.cs @@ -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(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 } }