From 4c441bd6986dcf50457c933ac3d9ece02bf27d24 Mon Sep 17 00:00:00 2001 From: BuknSS <284912933@qq.com> Date: Tue, 29 Oct 2024 18:59:56 +0800 Subject: [PATCH] Add support for Alipay's new user identity openid --- .../AlipayAuthenticationConstants.cs | 11 +++++++++++ .../AlipayAuthenticationHandler.cs | 2 -- .../AlipayAuthenticationOptions.cs | 9 +++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationConstants.cs b/src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationConstants.cs index d6188fc4f..1a2eeca51 100644 --- a/src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationConstants.cs +++ b/src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationConstants.cs @@ -25,5 +25,16 @@ public static class Claims /// The user's gender. F: Female; M: Male. /// public const string Gender = "urn:alipay:gender"; + + /// + /// OpenID is the unique identifier of Alipay users in the application dimension. + /// See https://opendocs.alipay.com/mini/0ai2i6 + /// + public const string OpenId = "urn:alipay:open_id"; + + /// + /// Alipay user system internal identifier, will no longer be independently open in the future, and will be replaced by OpenID. + /// + public const string UserId = "urn:alipay:user_id"; } } diff --git a/src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationHandler.cs b/src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationHandler.cs index 513bb3bf5..f01dd8b08 100644 --- a/src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationHandler.cs @@ -134,8 +134,6 @@ protected override async Task CreateTicketAsync( throw new AuthenticationFailureException($"An error (Code:{code}) occurred while retrieving user information."); } - identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, mainElement.GetString("user_id")!, ClaimValueTypes.String, Options.ClaimsIssuer)); - var principal = new ClaimsPrincipal(identity); var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, mainElement); diff --git a/src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationOptions.cs b/src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationOptions.cs index 08677ddd5..5beceb715 100644 --- a/src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationOptions.cs +++ b/src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationOptions.cs @@ -29,5 +29,14 @@ public AlipayAuthenticationOptions() ClaimActions.MapJsonKey(Claims.Gender, "gender"); ClaimActions.MapJsonKey(Claims.Nickname, "nick_name"); ClaimActions.MapJsonKey(Claims.Province, "province"); + ClaimActions.MapJsonKey(Claims.OpenId, "open_id"); + ClaimActions.MapJsonKey(Claims.UserId, "user_id"); + ClaimActions.MapCustomJson(System.Security.Claims.ClaimTypes.NameIdentifier, user => user.GetString(NameIdentifierKey)); } + + /// + /// Alipay user system internal identifier, which will no longer be open independently in the future and will be replaced by open_id. Currently the default is user_id + /// See https://opendocs.alipay.com/mini/0ai2i6?pathHash=13dd5946 + /// + public string NameIdentifierKey { get; set; } = "user_id"; }