@@ -43,82 +43,82 @@ public CustomUserManager(IUserStore<ApplicationUser> store, IOptions<IdentityOpt
43
43
_passwordHasher = passwordHasher ;
44
44
}
45
45
46
- public override async Task < ApplicationUser > FindByLoginAsync ( string loginProvider , string providerKey )
46
+ public override Task < ApplicationUser > FindByLoginAsync ( string loginProvider , string providerKey )
47
47
{
48
48
var cacheKey = CacheKey . With ( GetType ( ) , nameof ( FindByLoginAsync ) , loginProvider , providerKey ) ;
49
- var result = await _memoryCache . GetOrCreateExclusiveAsync ( cacheKey , async ( cacheEntry ) =>
49
+ return _memoryCache . GetOrCreateExclusiveAsync ( cacheKey , async cacheEntry =>
50
50
{
51
51
var user = await base . FindByLoginAsync ( loginProvider , providerKey ) ;
52
- if ( user != null )
52
+ if ( user is not null )
53
53
{
54
54
await LoadUserDetailsAsync ( user ) ;
55
- cacheEntry . AddExpirationToken ( SecurityCacheRegion . CreateChangeTokenForUser ( user ) ) ;
55
+ ConfigureCache ( cacheEntry , user ) ;
56
56
}
57
57
return user ;
58
58
} , cacheNullValue : false ) ;
59
-
60
- return result ;
61
59
}
62
60
63
- public override async Task < ApplicationUser > FindByEmailAsync ( string email )
61
+ public override Task < ApplicationUser > FindByEmailAsync ( string email )
64
62
{
65
63
var cacheKey = CacheKey . With ( GetType ( ) , nameof ( FindByEmailAsync ) , email ) ;
66
- var result = await _memoryCache . GetOrCreateExclusiveAsync ( cacheKey , async ( cacheEntry ) =>
64
+ return _memoryCache . GetOrCreateExclusiveAsync ( cacheKey , async cacheEntry =>
67
65
{
68
66
var user = await base . FindByEmailAsync ( email ) ;
69
- if ( user != null )
67
+ if ( user is not null )
70
68
{
71
69
await LoadUserDetailsAsync ( user ) ;
72
- cacheEntry . AddExpirationToken ( SecurityCacheRegion . CreateChangeTokenForUser ( user ) ) ;
70
+ ConfigureCache ( cacheEntry , user ) ;
73
71
}
74
72
return user ;
75
73
} , cacheNullValue : false ) ;
76
- return result ;
77
74
}
78
75
79
- public override async Task < ApplicationUser > FindByNameAsync ( string userName )
76
+ public override Task < ApplicationUser > FindByNameAsync ( string userName )
80
77
{
81
78
var cacheKey = CacheKey . With ( GetType ( ) , nameof ( FindByNameAsync ) , userName ) ;
82
- var result = await _memoryCache . GetOrCreateExclusiveAsync ( cacheKey , async ( cacheEntry ) =>
79
+ return _memoryCache . GetOrCreateExclusiveAsync ( cacheKey , async cacheEntry =>
83
80
{
84
81
var user = await base . FindByNameAsync ( userName ) ;
85
- if ( user != null )
82
+ if ( user is not null )
86
83
{
87
84
await LoadUserDetailsAsync ( user ) ;
88
- cacheEntry . AddExpirationToken ( SecurityCacheRegion . CreateChangeTokenForUser ( user ) ) ;
85
+ ConfigureCache ( cacheEntry , user ) ;
89
86
}
90
87
return user ;
91
88
} , cacheNullValue : false ) ;
92
- return result ;
93
89
}
94
90
95
- public override async Task < ApplicationUser > FindByIdAsync ( string userId )
91
+ public override Task < ApplicationUser > FindByIdAsync ( string userId )
96
92
{
97
93
var cacheKey = CacheKey . With ( GetType ( ) , nameof ( FindByIdAsync ) , userId ) ;
98
- var result = await _memoryCache . GetOrCreateExclusiveAsync ( cacheKey , async ( cacheEntry ) =>
94
+ return _memoryCache . GetOrCreateExclusiveAsync ( cacheKey , async cacheEntry =>
99
95
{
100
96
var user = await base . FindByIdAsync ( userId ) ;
101
- if ( user != null )
97
+ if ( user is not null )
102
98
{
103
99
await LoadUserDetailsAsync ( user ) ;
104
- cacheEntry . AddExpirationToken ( SecurityCacheRegion . CreateChangeTokenForUser ( user ) ) ;
100
+ ConfigureCache ( cacheEntry , user ) ;
105
101
}
106
102
return user ;
107
103
} , cacheNullValue : false ) ;
108
- return result ;
104
+ }
105
+
106
+ protected virtual void ConfigureCache ( MemoryCacheEntryOptions cacheOptions , ApplicationUser user )
107
+ {
108
+ cacheOptions . AddExpirationToken ( SecurityCacheRegion . CreateChangeTokenForUser ( user ) ) ;
109
109
}
110
110
111
111
public override Task < IdentityResult > ResetPasswordAsync ( ApplicationUser user , string token , string newPassword )
112
112
{
113
113
return UpdatePasswordAsync ( user , newPassword ,
114
- ( user , newPassword ) => base . ResetPasswordAsync ( user , token , newPassword ) ,
114
+ ( appUser , password ) => base . ResetPasswordAsync ( appUser , token , password ) ,
115
115
( userId , customPasswordHash ) => new UserResetPasswordEvent ( userId , customPasswordHash ) ) ;
116
116
}
117
117
118
118
public override Task < IdentityResult > ChangePasswordAsync ( ApplicationUser user , string currentPassword , string newPassword )
119
119
{
120
120
return UpdatePasswordAsync ( user , newPassword ,
121
- ( user , newPassword ) => base . ChangePasswordAsync ( user , currentPassword , newPassword ) ,
121
+ ( appUser , password ) => base . ChangePasswordAsync ( appUser , currentPassword , password ) ,
122
122
( userId , customPasswordHash ) => new UserChangedPasswordEvent ( userId , customPasswordHash ) ) ;
123
123
}
124
124
@@ -171,9 +171,10 @@ public override async Task<IdentityResult> DeleteAsync(ApplicationUser user)
171
171
{
172
172
var changedEntries = new List < GenericChangedEntry < ApplicationUser > >
173
173
{
174
- new GenericChangedEntry < ApplicationUser > ( user , EntryState . Deleted )
174
+ new ( user , EntryState . Deleted ) ,
175
175
} ;
176
176
await _eventPublisher . Publish ( new UserChangingEvent ( changedEntries ) ) ;
177
+
177
178
var result = await base . DeleteAsync ( user ) ;
178
179
if ( result . Succeeded )
179
180
{
@@ -185,18 +186,18 @@ public override async Task<IdentityResult> DeleteAsync(ApplicationUser user)
185
186
186
187
protected override async Task < IdentityResult > UpdateUserAsync ( ApplicationUser user )
187
188
{
188
- var newUser = ( ApplicationUser ) user . Clone ( ) ;
189
+ var newUser = user . CloneTyped ( ) ;
189
190
var existentUser = await LoadExistingUser ( user ) ;
190
191
191
192
//We cant update not existing user
192
- if ( existentUser == null )
193
+ if ( existentUser is null )
193
194
{
194
195
return IdentityResult . Failed ( ErrorDescriber . DefaultError ( ) ) ;
195
196
}
196
197
197
198
var changedEntries = new List < GenericChangedEntry < ApplicationUser > >
198
199
{
199
- new ( newUser , ( ApplicationUser ) existentUser . Clone ( ) , EntryState . Modified )
200
+ new ( newUser , existentUser . CloneTyped ( ) , EntryState . Modified ) ,
200
201
} ;
201
202
202
203
await _eventPublisher . Publish ( new UserChangingEvent ( changedEntries ) ) ;
@@ -233,7 +234,7 @@ public override async Task<IdentityResult> UpdateAsync(ApplicationUser user)
233
234
234
235
protected virtual async Task UpdateUserRolesAsync ( ApplicationUser user )
235
236
{
236
- if ( user . Roles == null )
237
+ if ( user . Roles is null )
237
238
{
238
239
return ;
239
240
}
@@ -256,7 +257,7 @@ protected virtual async Task UpdateUserRolesAsync(ApplicationUser user)
256
257
257
258
protected virtual async Task UpdateUserLoginsAsync ( ApplicationUser user )
258
259
{
259
- if ( user . Logins == null )
260
+ if ( user . Logins is null )
260
261
{
261
262
return ;
262
263
}
@@ -279,13 +280,14 @@ public override async Task<IdentityResult> CreateAsync(ApplicationUser user)
279
280
{
280
281
var changedEntries = new List < GenericChangedEntry < ApplicationUser > >
281
282
{
282
- new GenericChangedEntry < ApplicationUser > ( user , EntryState . Added )
283
+ new ( user , EntryState . Added ) ,
283
284
} ;
284
285
await _eventPublisher . Publish ( new UserChangingEvent ( changedEntries ) ) ;
286
+
285
287
var result = await base . CreateAsync ( user ) ;
286
288
if ( result . Succeeded )
287
289
{
288
- if ( ! user . Roles . IsNullOrEmpty ( ) )
290
+ if ( user . Roles ? . Count > 0 )
289
291
{
290
292
//Add
291
293
foreach ( var newRole in user . Roles )
@@ -295,7 +297,7 @@ public override async Task<IdentityResult> CreateAsync(ApplicationUser user)
295
297
}
296
298
297
299
// add external logins
298
- if ( ! user . Logins . IsNullOrEmpty ( ) )
300
+ if ( user . Logins ? . Length > 0 )
299
301
{
300
302
foreach ( var login in user . Logins )
301
303
{
@@ -306,6 +308,7 @@ public override async Task<IdentityResult> CreateAsync(ApplicationUser user)
306
308
SecurityCacheRegion . ExpireUser ( user ) ;
307
309
await _eventPublisher . Publish ( new UserChangedEvent ( changedEntries ) ) ;
308
310
}
311
+
309
312
return result ;
310
313
}
311
314
@@ -319,7 +322,6 @@ public override async Task<IdentityResult> CreateAsync(ApplicationUser user, str
319
322
}
320
323
321
324
return result ;
322
-
323
325
}
324
326
325
327
public override async Task < IdentityResult > AddToRoleAsync ( ApplicationUser user , string role )
@@ -329,6 +331,7 @@ public override async Task<IdentityResult> AddToRoleAsync(ApplicationUser user,
329
331
{
330
332
await _eventPublisher . Publish ( new UserRoleAddedEvent ( user , role ) ) ;
331
333
}
334
+
332
335
return result ;
333
336
}
334
337
@@ -339,6 +342,7 @@ public override async Task<IdentityResult> RemoveFromRoleAsync(ApplicationUser u
339
342
{
340
343
await _eventPublisher . Publish ( new UserRoleRemovedEvent ( user , role ) ) ;
341
344
}
345
+
342
346
return result ;
343
347
}
344
348
@@ -350,10 +354,7 @@ public override async Task<IdentityResult> RemoveFromRoleAsync(ApplicationUser u
350
354
/// <returns></returns>
351
355
protected virtual async Task LoadUserDetailsAsync ( ApplicationUser user )
352
356
{
353
- if ( user == null )
354
- {
355
- throw new ArgumentNullException ( nameof ( user ) ) ;
356
- }
357
+ ArgumentNullException . ThrowIfNull ( user ) ;
357
358
358
359
// check password expiry policy and mark password as expired, if needed
359
360
var lastPasswordChangeDate = user . LastPasswordChangedDate ?? user . CreatedDate ;
@@ -369,7 +370,7 @@ protected virtual async Task LoadUserDetailsAsync(ApplicationUser user)
369
370
foreach ( var roleName in await base . GetRolesAsync ( user ) )
370
371
{
371
372
var role = await _roleManager . FindByNameAsync ( roleName ) ;
372
- if ( role != null )
373
+ if ( role is not null )
373
374
{
374
375
user . Roles . Add ( role ) ;
375
376
}
@@ -397,13 +398,13 @@ protected virtual async Task<ApplicationUser> LoadExistingUser(ApplicationUser u
397
398
//It is important to call base.FindByIdAsync method to avoid of update a cached user.
398
399
result = await base . FindByIdAsync ( user . Id ) ;
399
400
}
400
- if ( result == null )
401
+ if ( result is null )
401
402
{
402
403
//It is important to call base.FindByNameAsync method to avoid of update a cached user.
403
404
result = await base . FindByNameAsync ( user . UserName ) ;
404
405
}
405
406
406
- if ( result != null )
407
+ if ( result is not null )
407
408
{
408
409
await LoadUserDetailsAsync ( result ) ;
409
410
}
0 commit comments