13
13
using Identity . DefaultUI . WebSite ;
14
14
using Identity . DefaultUI . WebSite . Data ;
15
15
using Microsoft . AspNetCore . Builder ;
16
- using Microsoft . AspNetCore . Hosting . Server ;
17
16
using Microsoft . AspNetCore . Http ;
18
17
using Microsoft . AspNetCore . Identity . EntityFrameworkCore ;
19
18
using Microsoft . AspNetCore . Identity . UI . Services ;
@@ -862,7 +861,7 @@ public async Task CanResetRecoveryCodes()
862
861
client . DefaultRequestHeaders . Authorization = new ( "Bearer" , recoveryAccessToken ) ;
863
862
864
863
var updated2faResponse = await client . PostAsJsonAsync ( "/identity/manage/2fa" , new object ( ) ) ;
865
- var updated2faContent = await updated2faResponse . Content . ReadFromJsonAsync < JsonElement > ( ) ; ;
864
+ var updated2faContent = await updated2faResponse . Content . ReadFromJsonAsync < JsonElement > ( ) ;
866
865
Assert . Equal ( 8 , updated2faContent . GetProperty ( "recoveryCodesLeft" ) . GetInt32 ( ) ) ;
867
866
Assert . Null ( updated2faContent . GetProperty ( "recoveryCodes" ) . GetString ( ) ) ;
868
867
@@ -1013,25 +1012,6 @@ public async Task CanResetPassword()
1013
1012
AssertOk ( await client . PostAsJsonAsync ( "/identity/login" , new { Email = confirmedEmail , Password = newPassword } ) ) ;
1014
1013
}
1015
1014
1016
- [ Fact ]
1017
- public async Task CanGetClaims ( )
1018
- {
1019
- await using var app = await CreateAppAsync ( ) ;
1020
- using var client = app . GetTestClient ( ) ;
1021
-
1022
- await RegisterAsync ( client ) ;
1023
- await LoginAsync ( client ) ;
1024
-
1025
- var infoResponse = await client . GetFromJsonAsync < JsonElement > ( "/identity/manage/info" ) ;
1026
- Assert . Equal ( Email , infoResponse . GetProperty ( "email" ) . GetString ( ) ) ;
1027
-
1028
- var claims = infoResponse . GetProperty ( "claims" ) ;
1029
- Assert . Equal ( Email , claims . GetProperty ( ClaimTypes . Name ) . GetString ( ) ) ;
1030
- Assert . Equal ( Email , claims . GetProperty ( ClaimTypes . Email ) . GetString ( ) ) ;
1031
- Assert . Equal ( "pwd" , claims . GetProperty ( "amr" ) . GetString ( ) ) ;
1032
- Assert . NotNull ( claims . GetProperty ( ClaimTypes . NameIdentifier ) . GetString ( ) ) ;
1033
- }
1034
-
1035
1015
[ Theory ]
1036
1016
[ MemberData ( nameof ( AddIdentityModes ) ) ]
1037
1017
public async Task CanChangeEmail ( string addIdentityModes )
@@ -1058,12 +1038,12 @@ public async Task CanChangeEmail(string addIdentityModes)
1058
1038
Assert . Equal ( Email , infoResponse . GetProperty ( "email" ) . GetString ( ) ) ;
1059
1039
Assert . True ( infoResponse . GetProperty ( "isEmailConfirmed" ) . GetBoolean ( ) ) ;
1060
1040
1061
- var infoClaims = infoResponse . GetProperty ( " claims") ;
1062
- Assert . Equal ( "pwd" , infoClaims . GetProperty ( "amr" ) . GetString ( ) ) ;
1063
- Assert . Equal ( Email , infoClaims . GetProperty ( ClaimTypes . Name ) . GetString ( ) ) ;
1064
- Assert . Equal ( Email , infoClaims . GetProperty ( ClaimTypes . Email ) . GetString ( ) ) ;
1041
+ var infoClaims = await client . GetFromJsonAsync < JsonElement > ( "/auth/ claims") ;
1042
+ Assert . Equal ( "pwd" , GetSingleClaim ( infoClaims , "amr" ) ) ;
1043
+ Assert . Equal ( Email , GetSingleClaim ( infoClaims , ClaimTypes . Name ) ) ;
1044
+ Assert . Equal ( Email , GetSingleClaim ( infoClaims , ClaimTypes . Email ) ) ;
1065
1045
1066
- var originalNameIdentifier = infoResponse . GetProperty ( "claims" ) . GetProperty ( ClaimTypes . NameIdentifier ) . GetString ( ) ;
1046
+ var originalNameIdentifier = GetSingleClaim ( infoClaims , ClaimTypes . NameIdentifier ) ;
1067
1047
var newEmail = $ "New-{ Email } ";
1068
1048
1069
1049
// The email must pass DataAnnotations validation by EmailAddressAttribute.
@@ -1077,10 +1057,10 @@ public async Task CanChangeEmail(string addIdentityModes)
1077
1057
Assert . True ( infoPostContent . GetProperty ( "isEmailConfirmed" ) . GetBoolean ( ) ) ;
1078
1058
1079
1059
// And none of the claims have yet been updated.
1080
- var infoPostClaims = infoPostContent . GetProperty ( " claims") ;
1081
- Assert . Equal ( Email , infoPostClaims . GetProperty ( ClaimTypes . Name ) . GetString ( ) ) ;
1082
- Assert . Equal ( Email , infoPostClaims . GetProperty ( ClaimTypes . Email ) . GetString ( ) ) ;
1083
- Assert . Equal ( originalNameIdentifier , infoClaims . GetProperty ( ClaimTypes . NameIdentifier ) . GetString ( ) ) ;
1060
+ var infoPostClaims = await client . GetFromJsonAsync < JsonElement > ( "/auth/ claims") ;
1061
+ Assert . Equal ( Email , GetSingleClaim ( infoPostClaims , ClaimTypes . Name ) ) ;
1062
+ Assert . Equal ( Email , GetSingleClaim ( infoPostClaims , ClaimTypes . Email ) ) ;
1063
+ Assert . Equal ( originalNameIdentifier , GetSingleClaim ( infoPostClaims , ClaimTypes . NameIdentifier ) ) ;
1084
1064
1085
1065
// We cannot log in with the new email until we confirm the email change.
1086
1066
await AssertProblemAsync ( await client . PostAsJsonAsync ( "/identity/login" , new { Email = newEmail , Password } ) ,
@@ -1103,10 +1083,10 @@ public async Task CanChangeEmail(string addIdentityModes)
1103
1083
Assert . Equal ( newEmail , infoAfterEmailChange . GetProperty ( "email" ) . GetString ( ) ) ;
1104
1084
1105
1085
// The email still won't be available as a claim until we get a new token.
1106
- var claimsAfterEmailChange = infoAfterEmailChange . GetProperty ( " claims") ;
1107
- Assert . Equal ( Email , claimsAfterEmailChange . GetProperty ( ClaimTypes . Name ) . GetString ( ) ) ;
1108
- Assert . Equal ( Email , claimsAfterEmailChange . GetProperty ( ClaimTypes . Email ) . GetString ( ) ) ;
1109
- Assert . Equal ( originalNameIdentifier , infoClaims . GetProperty ( ClaimTypes . NameIdentifier ) . GetString ( ) ) ;
1086
+ var claimsAfterEmailChange = await client . GetFromJsonAsync < JsonElement > ( "/auth/ claims") ;
1087
+ Assert . Equal ( Email , GetSingleClaim ( claimsAfterEmailChange , ClaimTypes . Name ) ) ;
1088
+ Assert . Equal ( Email , GetSingleClaim ( claimsAfterEmailChange , ClaimTypes . Email ) ) ;
1089
+ Assert . Equal ( originalNameIdentifier , GetSingleClaim ( claimsAfterEmailChange , ClaimTypes . NameIdentifier ) ) ;
1110
1090
1111
1091
// And now the email has changed, the refresh token is invalidated by the security stamp.
1112
1092
AssertUnauthorizedAndEmpty ( await client . PostAsJsonAsync ( "/identity/refresh" , new { RefreshToken = originalRefreshToken } ) ) ;
@@ -1118,10 +1098,10 @@ public async Task CanChangeEmail(string addIdentityModes)
1118
1098
Assert . Equal ( newEmail , infoAfterFinalLogin . GetProperty ( "email" ) . GetString ( ) ) ;
1119
1099
Assert . True ( infoAfterFinalLogin . GetProperty ( "isEmailConfirmed" ) . GetBoolean ( ) ) ;
1120
1100
1121
- var claimsAfterFinalLogin = infoAfterFinalLogin . GetProperty ( " claims") ;
1122
- Assert . Equal ( newEmail , claimsAfterFinalLogin . GetProperty ( ClaimTypes . Name ) . GetString ( ) ) ;
1123
- Assert . Equal ( newEmail , claimsAfterFinalLogin . GetProperty ( ClaimTypes . Email ) . GetString ( ) ) ;
1124
- Assert . Equal ( originalNameIdentifier , infoClaims . GetProperty ( ClaimTypes . NameIdentifier ) . GetString ( ) ) ;
1101
+ var claimsAfterFinalLogin = await client . GetFromJsonAsync < JsonElement > ( "/auth/ claims") ;
1102
+ Assert . Equal ( newEmail , GetSingleClaim ( claimsAfterFinalLogin , ClaimTypes . Name ) ) ;
1103
+ Assert . Equal ( newEmail , GetSingleClaim ( claimsAfterFinalLogin , ClaimTypes . Email ) ) ;
1104
+ Assert . Equal ( originalNameIdentifier , GetSingleClaim ( claimsAfterFinalLogin , ClaimTypes . NameIdentifier ) ) ;
1125
1105
}
1126
1106
1127
1107
[ Fact ]
@@ -1152,12 +1132,13 @@ public async Task CannotUpdateClaimsDuringInfoPostWithCookies()
1152
1132
1153
1133
var infoResponse = await client . GetFromJsonAsync < JsonElement > ( "/identity/manage/info" ) ;
1154
1134
Assert . Equal ( Email , infoResponse . GetProperty ( "email" ) . GetString ( ) ) ;
1155
- var infoClaims = infoResponse . GetProperty ( "claims" ) ;
1156
- Assert . Equal ( "pwd" , infoClaims . GetProperty ( "amr" ) . GetString ( ) ) ;
1157
- Assert . Equal ( Email , infoClaims . GetProperty ( ClaimTypes . Name ) . GetString ( ) ) ;
1158
- Assert . Equal ( Email , infoClaims . GetProperty ( ClaimTypes . Email ) . GetString ( ) ) ;
1159
1135
1160
- var originalNameIdentifier = infoResponse . GetProperty ( "claims" ) . GetProperty ( ClaimTypes . NameIdentifier ) . GetString ( ) ;
1136
+ var infoClaims = await client . GetFromJsonAsync < JsonElement > ( "/auth/claims" ) ;
1137
+ Assert . Equal ( "pwd" , GetSingleClaim ( infoClaims , "amr" ) ) ;
1138
+ Assert . Equal ( Email , GetSingleClaim ( infoClaims , ClaimTypes . Name ) ) ;
1139
+ Assert . Equal ( Email , GetSingleClaim ( infoClaims , ClaimTypes . Email ) ) ;
1140
+
1141
+ var originalNameIdentifier = GetSingleClaim ( infoClaims , ClaimTypes . NameIdentifier ) ;
1161
1142
var newEmail = $ "NewEmailPrefix-{ Email } ";
1162
1143
1163
1144
var infoPostResponse = await client . PostAsJsonAsync ( "/identity/manage/info" , new { newEmail } ) ;
@@ -1169,9 +1150,9 @@ public async Task CannotUpdateClaimsDuringInfoPostWithCookies()
1169
1150
Assert . Equal ( Email , infoPostContent . GetProperty ( "email" ) . GetString ( ) ) ;
1170
1151
1171
1152
// The claims have not been updated to match.
1172
- var infoPostClaims = infoPostContent . GetProperty ( " claims") ;
1173
- Assert . Equal ( Email , infoPostClaims . GetProperty ( ClaimTypes . Email ) . GetString ( ) ) ;
1174
- Assert . Equal ( originalNameIdentifier , infoClaims . GetProperty ( ClaimTypes . NameIdentifier ) . GetString ( ) ) ;
1153
+ var infoPostClaims = await client . GetFromJsonAsync < JsonElement > ( "/auth/ claims") ;
1154
+ Assert . Equal ( Email , GetSingleClaim ( infoPostClaims , ClaimTypes . Email ) ) ;
1155
+ Assert . Equal ( originalNameIdentifier , GetSingleClaim ( infoPostClaims , ClaimTypes . NameIdentifier ) ) ;
1175
1156
1176
1157
// Two emails have now been sent. The first was sent during registration. And the second for the email change.
1177
1158
Assert . Equal ( 2 , emailSender . Emails . Count ) ;
@@ -1191,9 +1172,9 @@ public async Task CannotUpdateClaimsDuringInfoPostWithCookies()
1191
1172
Assert . Equal ( newEmail , infoAfterEmailChange . GetProperty ( "email" ) . GetString ( ) ) ;
1192
1173
1193
1174
// The email still won't be available as a claim until we get a new cookie.
1194
- var claimsAfterEmailChange = infoAfterEmailChange . GetProperty ( " claims") ;
1195
- Assert . Equal ( Email , claimsAfterEmailChange . GetProperty ( ClaimTypes . Email ) . GetString ( ) ) ;
1196
- Assert . Equal ( originalNameIdentifier , infoClaims . GetProperty ( ClaimTypes . NameIdentifier ) . GetString ( ) ) ;
1175
+ var claimsAfterEmailChange = await client . GetFromJsonAsync < JsonElement > ( "/auth/ claims") ;
1176
+ Assert . Equal ( Email , GetSingleClaim ( claimsAfterEmailChange , ClaimTypes . Email ) ) ;
1177
+ Assert . Equal ( originalNameIdentifier , GetSingleClaim ( claimsAfterEmailChange , ClaimTypes . NameIdentifier ) ) ;
1197
1178
1198
1179
// We will finally see all the claims updated after logging in again.
1199
1180
var secondLoginResponse = await client . PostAsJsonAsync ( "/identity/login?useCookies=true" , new { Email = newEmail , Password } ) ;
@@ -1202,10 +1183,10 @@ public async Task CannotUpdateClaimsDuringInfoPostWithCookies()
1202
1183
var infoAfterFinalLogin = await client . GetFromJsonAsync < JsonElement > ( "/identity/manage/info" ) ;
1203
1184
Assert . Equal ( newEmail , infoAfterFinalLogin . GetProperty ( "email" ) . GetString ( ) ) ;
1204
1185
1205
- var claimsAfterFinalLogin = infoAfterFinalLogin . GetProperty ( " claims") ;
1206
- Assert . Equal ( newEmail , claimsAfterFinalLogin . GetProperty ( ClaimTypes . Name ) . GetString ( ) ) ;
1207
- Assert . Equal ( newEmail , claimsAfterFinalLogin . GetProperty ( ClaimTypes . Email ) . GetString ( ) ) ;
1208
- Assert . Equal ( originalNameIdentifier , infoClaims . GetProperty ( ClaimTypes . NameIdentifier ) . GetString ( ) ) ;
1186
+ var claimsAfterFinalLogin = await client . GetFromJsonAsync < JsonElement > ( "/auth/ claims") ;
1187
+ Assert . Equal ( newEmail , GetSingleClaim ( claimsAfterFinalLogin , ClaimTypes . Name ) ) ;
1188
+ Assert . Equal ( newEmail , GetSingleClaim ( claimsAfterFinalLogin , ClaimTypes . Email ) ) ;
1189
+ Assert . Equal ( originalNameIdentifier , GetSingleClaim ( claimsAfterFinalLogin , ClaimTypes . NameIdentifier ) ) ;
1209
1190
}
1210
1191
1211
1192
[ Fact ]
@@ -1321,6 +1302,8 @@ private async Task<WebApplication> CreateAppAsync<TUser, TContext>(Action<IServi
1321
1302
authGroup . MapGet ( "/hello" ,
1322
1303
( ClaimsPrincipal user ) => $ "Hello, { user . Identity ? . Name } !") ;
1323
1304
1305
+ authGroup . MapGet ( "/claims" , ( ClaimsPrincipal user ) => user . Claims . Select ( c => new { c . Type , c . Value } ) ) ;
1306
+
1324
1307
await dbConnection . OpenAsync ( ) ;
1325
1308
await app . Services . GetRequiredService < TContext > ( ) . Database . EnsureCreatedAsync ( ) ;
1326
1309
@@ -1367,6 +1350,9 @@ private Task<WebApplication> CreateAppAsync(Action<IServiceCollection>? configur
1367
1350
1368
1351
public static object [ ] [ ] AddIdentityModes => AddIdentityActions . Keys . Select ( key => new object [ ] { key } ) . ToArray ( ) ;
1369
1352
1353
+ private static string ? GetSingleClaim ( JsonElement claims , string name )
1354
+ => claims . EnumerateArray ( ) . Single ( e => e . GetProperty ( "type" ) . GetString ( ) == name ) . GetProperty ( "value" ) . GetString ( ) ;
1355
+
1370
1356
private static string GetEmailConfirmationLink ( TestEmail email )
1371
1357
{
1372
1358
// Update if we add more links to the email.
0 commit comments