@@ -81,9 +81,9 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
8181 using var document = await JsonDocument . ParseAsync ( stream ) ;
8282
8383 var mainElement = document . RootElement . GetProperty ( "alipay_system_oauth_token_response" ) ;
84- if ( ! ValidateReturnCode ( mainElement , out var code ) )
84+ if ( ! ValidateReturnCode ( mainElement , out var code , out var subCode ) )
8585 {
86- return OAuthTokenResponse . Failed ( new Exception ( $ "An error (Code:{ code } ) occurred while retrieving an access token.") ) ;
86+ return OAuthTokenResponse . Failed ( new Exception ( $ "An error (Code:{ code } subCode: { subCode } ) occurred while retrieving an access token.") ) ;
8787 }
8888
8989 var payload = JsonDocument . Parse ( mainElement . GetRawText ( ) ) ;
@@ -126,15 +126,16 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
126126 if ( ! rootElement . TryGetProperty ( "alipay_user_info_share_response" , out JsonElement mainElement ) )
127127 {
128128 var errorCode = rootElement . GetProperty ( "error_response" ) . GetProperty ( "code" ) . GetString ( ) ! ;
129- throw new AuthenticationFailureException ( $ "An error (Code:{ errorCode } ) occurred while retrieving user information.") ;
129+ var errorSubCode = rootElement . GetProperty ( "error_response" ) . GetProperty ( "sub_code" ) . GetString ( ) ! ;
130+ throw new AuthenticationFailureException ( $ "An error response (Code:{ errorCode } subCode:{ errorSubCode } ) occurred while retrieving user information.") ;
130131 }
131132
132- if ( ! ValidateReturnCode ( mainElement , out var code ) )
133+ if ( ! ValidateReturnCode ( mainElement , out var code , out var subCode ) )
133134 {
134- throw new AuthenticationFailureException ( $ "An error (Code:{ code } ) occurred while retrieving user information.") ;
135+ throw new AuthenticationFailureException ( $ "An error (Code:{ code } subCode: { subCode } ) occurred while retrieving user information.") ;
135136 }
136137
137- identity . AddClaim ( new Claim ( ClaimTypes . NameIdentifier , mainElement . GetString ( "user_id" ) ! , ClaimValueTypes . String , Options . ClaimsIssuer ) ) ;
138+ identity . AddClaim ( new Claim ( ClaimTypes . NameIdentifier , GetUserIdentifier ( mainElement ) , ClaimValueTypes . String , Options . ClaimsIssuer ) ) ;
138139
139140 var principal = new ClaimsPrincipal ( identity ) ;
140141 var context = new OAuthCreatingTicketContext ( principal , properties , Context , Scheme , Options , Backchannel , tokens , mainElement ) ;
@@ -153,17 +154,28 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
153154 /// </summary>
154155 /// <param name="element">Main part of json document from response</param>
155156 /// <param name="code">Returned code from server</param>
157+ /// <param name="subCode">Returned sub_code from server</param>
156158 /// <remarks>See https://opendocs.alipay.com/open/common/105806 for details.</remarks>
157159 /// <returns>True if succeed, otherwise false.</returns>
158- private static bool ValidateReturnCode ( JsonElement element , out string code )
160+ private static bool ValidateReturnCode ( JsonElement element , out string code , out string subCode )
159161 {
160162 if ( ! element . TryGetProperty ( "code" , out JsonElement codeElement ) )
161163 {
162164 code = string . Empty ;
165+ subCode = string . Empty ;
163166 return true ;
164167 }
165168
166169 code = codeElement . GetString ( ) ! ;
170+
171+ if ( ! element . TryGetProperty ( "sub_code" , out JsonElement subCodeElement ) )
172+ {
173+ subCode = string . Empty ;
174+ return true ;
175+ }
176+
177+ subCode = subCodeElement . GetString ( ) ! ;
178+
167179 return code == "10000" ;
168180 }
169181
@@ -200,6 +212,22 @@ private string GetRSA2Signature([NotNull] SortedDictionary<string, string?> sort
200212 return Convert . ToBase64String ( encryptedBytes ) ;
201213 }
202214
215+ /// <summary>
216+ /// Get user identifier from response.
217+ /// </summary>
218+ /// <param name="element">Main part of json document from response</param>
219+ /// <remarks>See https://opendocs.alipay.com/common/0ai2i6?pathHash=cba76ebf for details.</remarks>
220+ /// <returns>UserId or OpenId</returns>
221+ private static string GetUserIdentifier ( JsonElement element )
222+ {
223+ if ( element . TryGetProperty ( "user_id" , out JsonElement userIdElement ) )
224+ {
225+ return userIdElement . GetString ( ) ! ;
226+ }
227+
228+ return element . GetString ( "open_id" ) ! ;
229+ }
230+
203231 /// <inheritdoc />
204232 protected override string BuildChallengeUrl ( [ NotNull ] AuthenticationProperties properties , [ NotNull ] string redirectUri )
205233 {
0 commit comments