|
|
|
@ -20,11 +20,11 @@ namespace Volo.Abp.IdentityServer.AspNetIdentity
|
|
|
|
|
{
|
|
|
|
|
public class AbpResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
|
|
|
|
|
{
|
|
|
|
|
private readonly SignInManager<IdentityUser> _signInManager;
|
|
|
|
|
private readonly IEventService _events;
|
|
|
|
|
private readonly UserManager<IdentityUser> _userManager;
|
|
|
|
|
private readonly ILogger<ResourceOwnerPasswordValidator<IdentityUser>> _logger;
|
|
|
|
|
private readonly IStringLocalizer<AbpIdentityServerResource> _localizer;
|
|
|
|
|
protected SignInManager<IdentityUser> SignInManager { get; }
|
|
|
|
|
protected IEventService Events { get; }
|
|
|
|
|
protected UserManager<IdentityUser> UserManager { get; }
|
|
|
|
|
protected ILogger<ResourceOwnerPasswordValidator<IdentityUser>> Logger { get; }
|
|
|
|
|
protected IStringLocalizer<AbpIdentityServerResource> Localizer { get; }
|
|
|
|
|
|
|
|
|
|
public AbpResourceOwnerPasswordValidator(
|
|
|
|
|
UserManager<IdentityUser> userManager,
|
|
|
|
@ -33,11 +33,11 @@ namespace Volo.Abp.IdentityServer.AspNetIdentity
|
|
|
|
|
ILogger<ResourceOwnerPasswordValidator<IdentityUser>> logger,
|
|
|
|
|
IStringLocalizer<AbpIdentityServerResource> localizer)
|
|
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_signInManager = signInManager;
|
|
|
|
|
_events = events;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_localizer = localizer;
|
|
|
|
|
UserManager = userManager;
|
|
|
|
|
SignInManager = signInManager;
|
|
|
|
|
Events = events;
|
|
|
|
|
Logger = logger;
|
|
|
|
|
Localizer = localizer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -49,17 +49,17 @@ namespace Volo.Abp.IdentityServer.AspNetIdentity
|
|
|
|
|
public virtual async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
|
|
|
|
|
{
|
|
|
|
|
await ReplaceEmailToUsernameOfInputIfNeeds(context);
|
|
|
|
|
var user = await _userManager.FindByNameAsync(context.UserName);
|
|
|
|
|
var user = await UserManager.FindByNameAsync(context.UserName);
|
|
|
|
|
string errorDescription;
|
|
|
|
|
if (user != null)
|
|
|
|
|
{
|
|
|
|
|
var result = await _signInManager.CheckPasswordSignInAsync(user, context.Password, true);
|
|
|
|
|
var result = await SignInManager.CheckPasswordSignInAsync(user, context.Password, true);
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
var sub = await _userManager.GetUserIdAsync(user);
|
|
|
|
|
var sub = await UserManager.GetUserIdAsync(user);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Credentials validated for username: {username}", context.UserName);
|
|
|
|
|
await _events.RaiseAsync(new UserLoginSuccessEvent(context.UserName, sub, context.UserName, interactive: false));
|
|
|
|
|
Logger.LogInformation("Credentials validated for username: {username}", context.UserName);
|
|
|
|
|
await Events.RaiseAsync(new UserLoginSuccessEvent(context.UserName, sub, context.UserName, interactive: false));
|
|
|
|
|
|
|
|
|
|
var additionalClaims = new List<Claim>();
|
|
|
|
|
|
|
|
|
@ -75,28 +75,28 @@ namespace Volo.Abp.IdentityServer.AspNetIdentity
|
|
|
|
|
}
|
|
|
|
|
else if (result.IsLockedOut)
|
|
|
|
|
{
|
|
|
|
|
_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"];
|
|
|
|
|
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"];
|
|
|
|
|
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"];
|
|
|
|
|
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"];
|
|
|
|
|
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, errorDescription);
|
|
|
|
@ -109,13 +109,13 @@ namespace Volo.Abp.IdentityServer.AspNetIdentity
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var userByUsername = await _userManager.FindByNameAsync(context.UserName);
|
|
|
|
|
var userByUsername = await UserManager.FindByNameAsync(context.UserName);
|
|
|
|
|
if (userByUsername != null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var userByEmail = await _userManager.FindByEmailAsync(context.UserName);
|
|
|
|
|
var userByEmail = await UserManager.FindByEmailAsync(context.UserName);
|
|
|
|
|
if (userByEmail == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|