@@ -80,7 +80,6 @@ public SecurityController(
80
80
}
81
81
82
82
private UserManager < ApplicationUser > UserManager => _signInManager . UserManager ;
83
- private string CurrentUserName => User ? . Identity ? . Name ;
84
83
85
84
private readonly string UserNotFound = "User not found." ;
86
85
private readonly string UserForbiddenToEdit = "It is forbidden to edit this user." ;
@@ -143,11 +142,12 @@ public async Task<ActionResult<SignInResult>> Login([FromBody] LoginRequest requ
143
142
/// </summary>
144
143
[ HttpGet ]
145
144
[ Authorize ]
145
+ [ AllowAnonymous ]
146
146
[ Route ( "logout" ) ]
147
147
[ ProducesResponseType ( typeof ( void ) , StatusCodes . Status204NoContent ) ]
148
148
public async Task < ActionResult > Logout ( )
149
149
{
150
- var user = await UserManager . FindByNameAsync ( CurrentUserName ) ;
150
+ var user = await GetCurrentUserAsync ( ) ;
151
151
if ( user != null )
152
152
{
153
153
await _signInManager . SignOutAsync ( ) ;
@@ -166,15 +166,10 @@ public async Task<ActionResult> Logout()
166
166
[ Route ( "currentuser" ) ]
167
167
public async Task < ActionResult < UserDetail > > GetCurrentUser ( )
168
168
{
169
- if ( User . Identity ? . IsAuthenticated != true )
170
- {
171
- return Ok ( new { } ) ;
172
- }
173
-
174
- var user = await UserManager . FindByNameAsync ( CurrentUserName ) ;
169
+ var user = await GetCurrentUserAsync ( ) ;
175
170
if ( user == null )
176
171
{
177
- return NotFound ( ) ;
172
+ return Ok ( new { } ) ;
178
173
}
179
174
180
175
var result = new UserDetail
@@ -454,7 +449,7 @@ public async Task<ActionResult<SecurityResult>> ChangeCurrentUserPassword([FromB
454
449
[ Authorize ( PlatformPermissions . SecurityUpdate ) ]
455
450
public async Task < ActionResult < SecurityResult > > ChangePassword ( [ FromRoute ] string userName , [ FromBody ] ChangePasswordRequest changePassword )
456
451
{
457
- var currentUser = await UserManager . FindByNameAsync ( CurrentUserName ) ;
452
+ var currentUser = await GetCurrentUserAsync ( ) ;
458
453
if ( currentUser == null )
459
454
{
460
455
throw new PlatformException ( "Can't find current user." ) ;
@@ -509,7 +504,7 @@ public async Task<ActionResult<SecurityResult>> ChangePassword([FromRoute] strin
509
504
[ Authorize ( PlatformPermissions . SecurityUpdate ) ]
510
505
public async Task < ActionResult < SecurityResult > > ResetPassword ( [ FromRoute ] string userName , [ FromBody ] ResetPasswordConfirmRequest resetPasswordConfirm )
511
506
{
512
- var currentUser = await UserManager . FindByNameAsync ( CurrentUserName ) ;
507
+ var currentUser = await GetCurrentUserAsync ( ) ;
513
508
if ( currentUser == null )
514
509
{
515
510
throw new PlatformException ( "Can't find current user." ) ;
@@ -668,12 +663,7 @@ public async Task<ActionResult> RequestPasswordReset(string loginOrEmail)
668
663
[ AllowAnonymous ]
669
664
public async Task < ActionResult < IdentityResult > > ValidatePassword ( [ FromBody ] string password )
670
665
{
671
- ApplicationUser user = null ;
672
- if ( User . Identity ? . IsAuthenticated == true )
673
- {
674
- user = await UserManager . FindByNameAsync ( User . Identity . Name ) ;
675
- }
676
-
666
+ var user = await GetCurrentUserAsync ( ) ;
677
667
var result = await ValidatePassword ( user , password ) ;
678
668
679
669
return Ok ( result ) ;
@@ -824,7 +814,7 @@ public async Task<ActionResult<UserLockedResult>> PasswordChangeEnabled()
824
814
{
825
815
var result = new PasswordChangeEnabledResult ( true ) ;
826
816
827
- var currentUser = await UserManager . FindByNameAsync ( CurrentUserName ) ;
817
+ var currentUser = await GetCurrentUserAsync ( ) ;
828
818
if ( currentUser ? . IsAdministrator == true )
829
819
{
830
820
result . Enabled = _passwordOptions . PasswordChangeByAdminEnabled ;
@@ -1061,6 +1051,17 @@ public async Task<ActionResult<bool>> VerifyUserToken([FromRoute] string userId,
1061
1051
return Ok ( success ) ;
1062
1052
}
1063
1053
1054
+ private Task < ApplicationUser > GetCurrentUserAsync ( )
1055
+ {
1056
+ if ( string . IsNullOrEmpty ( User . Identity ? . Name ) ||
1057
+ ! User . Identity . IsAuthenticated )
1058
+ {
1059
+ return Task . FromResult < ApplicationUser > ( null ) ;
1060
+ }
1061
+
1062
+ return UserManager . FindByNameAsync ( User . Identity . Name ) ;
1063
+ }
1064
+
1064
1065
private bool IsUserEditable ( string userName )
1065
1066
{
1066
1067
return _securityOptions . NonEditableUsers ? . FirstOrDefault ( x => x . EqualsInvariant ( userName ) ) == null ;
0 commit comments