@@ -81,9 +81,9 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
81
81
using var document = await JsonDocument . ParseAsync ( stream ) ;
82
82
83
83
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 ) )
85
85
{
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.") ) ;
87
87
}
88
88
89
89
var payload = JsonDocument . Parse ( mainElement . GetRawText ( ) ) ;
@@ -126,15 +126,16 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
126
126
if ( ! rootElement . TryGetProperty ( "alipay_user_info_share_response" , out JsonElement mainElement ) )
127
127
{
128
128
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.") ;
130
131
}
131
132
132
- if ( ! ValidateReturnCode ( mainElement , out var code ) )
133
+ if ( ! ValidateReturnCode ( mainElement , out var code , out var subCode ) )
133
134
{
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.") ;
135
136
}
136
137
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 ) ) ;
138
139
139
140
var principal = new ClaimsPrincipal ( identity ) ;
140
141
var context = new OAuthCreatingTicketContext ( principal , properties , Context , Scheme , Options , Backchannel , tokens , mainElement ) ;
@@ -153,17 +154,28 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
153
154
/// </summary>
154
155
/// <param name="element">Main part of json document from response</param>
155
156
/// <param name="code">Returned code from server</param>
157
+ /// <param name="subCode">Returned sub_code from server</param>
156
158
/// <remarks>See https://opendocs.alipay.com/open/common/105806 for details.</remarks>
157
159
/// <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 )
159
161
{
160
162
if ( ! element . TryGetProperty ( "code" , out JsonElement codeElement ) )
161
163
{
162
164
code = string . Empty ;
165
+ subCode = string . Empty ;
163
166
return true ;
164
167
}
165
168
166
169
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
+
167
179
return code == "10000" ;
168
180
}
169
181
@@ -200,6 +212,22 @@ private string GetRSA2Signature([NotNull] SortedDictionary<string, string?> sort
200
212
return Convert . ToBase64String ( encryptedBytes ) ;
201
213
}
202
214
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
+
203
231
/// <inheritdoc />
204
232
protected override string BuildChallengeUrl ( [ NotNull ] AuthenticationProperties properties , [ NotNull ] string redirectUri )
205
233
{
0 commit comments