|
|
|
@ -8,7 +8,9 @@ using IdentityServer4.Models; |
|
|
|
using IdentityServer4.Services; |
|
|
|
using IdentityServer4.Validation; |
|
|
|
using Microsoft.AspNetCore.Identity; |
|
|
|
using Microsoft.Extensions.Localization; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using Volo.Abp.IdentityServer.Localization; |
|
|
|
using Volo.Abp.Security.Claims; |
|
|
|
using Volo.Abp.Uow; |
|
|
|
using Volo.Abp.Validation; |
|
|
|
@ -22,17 +24,20 @@ namespace Volo.Abp.IdentityServer.AspNetIdentity |
|
|
|
private readonly IEventService _events; |
|
|
|
private readonly UserManager<IdentityUser> _userManager; |
|
|
|
private readonly ILogger<ResourceOwnerPasswordValidator<IdentityUser>> _logger; |
|
|
|
private readonly IStringLocalizer<AbpIdentityServerResource> _localizer; |
|
|
|
|
|
|
|
public AbpResourceOwnerPasswordValidator( |
|
|
|
UserManager<IdentityUser> userManager, |
|
|
|
SignInManager<IdentityUser> signInManager, |
|
|
|
IEventService events, |
|
|
|
ILogger<ResourceOwnerPasswordValidator<IdentityUser>> logger) |
|
|
|
ILogger<ResourceOwnerPasswordValidator<IdentityUser>> logger, |
|
|
|
IStringLocalizer<AbpIdentityServerResource> localizer) |
|
|
|
{ |
|
|
|
_userManager = userManager; |
|
|
|
_signInManager = signInManager; |
|
|
|
_events = events; |
|
|
|
_logger = logger; |
|
|
|
_localizer = localizer; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -44,8 +49,8 @@ namespace Volo.Abp.IdentityServer.AspNetIdentity |
|
|
|
public virtual async Task ValidateAsync(ResourceOwnerPasswordValidationContext context) |
|
|
|
{ |
|
|
|
await ReplaceEmailToUsernameOfInputIfNeeds(context); |
|
|
|
|
|
|
|
var user = await _userManager.FindByNameAsync(context.UserName); |
|
|
|
string errorDescription; |
|
|
|
if (user != null) |
|
|
|
{ |
|
|
|
var result = await _signInManager.CheckPasswordSignInAsync(user, context.Password, true); |
|
|
|
@ -72,25 +77,29 @@ namespace Volo.Abp.IdentityServer.AspNetIdentity |
|
|
|
{ |
|
|
|
_logger.LogInformation("Authentication failed for username: {username}, reason: locked out", context.UserName); |
|
|
|
await _events.RaiseAsync(new UserLoginFailureEvent(context.UserName, "locked out", interactive: false)); |
|
|
|
errorDescription = _localizer["UserLockedOut"]; |
|
|
|
} |
|
|
|
else if (result.IsNotAllowed) |
|
|
|
{ |
|
|
|
_logger.LogInformation("Authentication failed for username: {username}, reason: not allowed", context.UserName); |
|
|
|
await _events.RaiseAsync(new UserLoginFailureEvent(context.UserName, "not allowed", interactive: false)); |
|
|
|
errorDescription = _localizer["LoginIsNotAllowed"]; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
_logger.LogInformation("Authentication failed for username: {username}, reason: invalid credentials", context.UserName); |
|
|
|
await _events.RaiseAsync(new UserLoginFailureEvent(context.UserName, "invalid credentials", interactive: false)); |
|
|
|
errorDescription = _localizer["InvalidUserNameOrPassword"]; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
_logger.LogInformation("No user found matching username: {username}", context.UserName); |
|
|
|
await _events.RaiseAsync(new UserLoginFailureEvent(context.UserName, "invalid username", interactive: false)); |
|
|
|
errorDescription = _localizer["InvalidUsername"]; |
|
|
|
} |
|
|
|
|
|
|
|
context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant); |
|
|
|
context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, errorDescription); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual async Task ReplaceEmailToUsernameOfInputIfNeeds(ResourceOwnerPasswordValidationContext context) |
|
|
|
|