mirror of https://github.com/abpframework/abp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
4.8 KiB
155 lines
4.8 KiB
using System;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Volo.Abp.Account.Localization;
|
|
using Volo.Abp.Account.Settings;
|
|
using Volo.Abp.Account.Web.Areas.Account.Controllers.Models;
|
|
using Volo.Abp.AspNetCore.Mvc;
|
|
using Volo.Abp.Identity;
|
|
using Volo.Abp.Settings;
|
|
using Volo.Abp.Validation;
|
|
using SignInResult = Microsoft.AspNetCore.Identity.SignInResult;
|
|
using UserLoginInfo = Volo.Abp.Account.Web.Areas.Account.Controllers.Models.UserLoginInfo;
|
|
using IdentityUser = Volo.Abp.Identity.IdentityUser;
|
|
|
|
namespace Volo.Abp.Account.Web.Areas.Account.Controllers
|
|
{
|
|
[RemoteService]
|
|
[Controller]
|
|
[ControllerName("Login")]
|
|
[Area("Account")]
|
|
[Route("api/account")]
|
|
public class AccountController : AbpController
|
|
{
|
|
protected SignInManager<IdentityUser> SignInManager { get; }
|
|
protected IdentityUserManager UserManager { get; }
|
|
protected ISettingProvider SettingProvider { get; }
|
|
|
|
public AccountController(SignInManager<IdentityUser> signInManager, IdentityUserManager userManager, ISettingProvider settingProvider)
|
|
{
|
|
LocalizationResource = typeof(AccountResource);
|
|
|
|
SignInManager = signInManager;
|
|
UserManager = userManager;
|
|
SettingProvider = settingProvider;
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("login")]
|
|
public virtual async Task<AbpLoginResult> Login(UserLoginInfo login)
|
|
{
|
|
await CheckLocalLoginAsync();
|
|
|
|
ValidateLoginInfo(login);
|
|
|
|
await ReplaceEmailToUsernameOfInputIfNeeds(login);
|
|
|
|
return GetAbpLoginResult(await SignInManager.PasswordSignInAsync(
|
|
login.UserNameOrEmailAddress,
|
|
login.Password,
|
|
login.RememberMe,
|
|
true
|
|
));
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("logout")]
|
|
public virtual async Task Logout()
|
|
{
|
|
await SignInManager.SignOutAsync();
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("checkPassword")]
|
|
public virtual async Task<AbpLoginResult> CheckPassword(UserLoginInfo login)
|
|
{
|
|
ValidateLoginInfo(login);
|
|
|
|
await ReplaceEmailToUsernameOfInputIfNeeds(login);
|
|
|
|
var identityUser = await UserManager.FindByNameAsync(login.UserNameOrEmailAddress);
|
|
|
|
if (identityUser == null)
|
|
{
|
|
return new AbpLoginResult(LoginResultType.InvalidUserNameOrPassword);
|
|
}
|
|
|
|
return GetAbpLoginResult(await SignInManager.CheckPasswordSignInAsync(identityUser, login.Password, true));
|
|
}
|
|
|
|
protected virtual async Task ReplaceEmailToUsernameOfInputIfNeeds(UserLoginInfo login)
|
|
{
|
|
if (!ValidationHelper.IsValidEmailAddress(login.UserNameOrEmailAddress))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var userByUsername = await UserManager.FindByNameAsync(login.UserNameOrEmailAddress);
|
|
if (userByUsername != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var userByEmail = await UserManager.FindByEmailAsync(login.UserNameOrEmailAddress);
|
|
if (userByEmail == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
login.UserNameOrEmailAddress = userByEmail.UserName;
|
|
}
|
|
|
|
private static AbpLoginResult GetAbpLoginResult(SignInResult result)
|
|
{
|
|
if (result.IsLockedOut)
|
|
{
|
|
return new AbpLoginResult(LoginResultType.LockedOut);
|
|
}
|
|
|
|
if (result.RequiresTwoFactor)
|
|
{
|
|
return new AbpLoginResult(LoginResultType.RequiresTwoFactor);
|
|
}
|
|
|
|
if (result.IsNotAllowed)
|
|
{
|
|
return new AbpLoginResult(LoginResultType.NotAllowed);
|
|
}
|
|
|
|
if (!result.Succeeded)
|
|
{
|
|
return new AbpLoginResult(LoginResultType.InvalidUserNameOrPassword);
|
|
}
|
|
|
|
return new AbpLoginResult(LoginResultType.Success);
|
|
}
|
|
|
|
protected virtual void ValidateLoginInfo(UserLoginInfo login)
|
|
{
|
|
if (login == null)
|
|
{
|
|
throw new ArgumentException(nameof(login));
|
|
}
|
|
|
|
if (login.UserNameOrEmailAddress.IsNullOrEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(login.UserNameOrEmailAddress));
|
|
}
|
|
|
|
if (login.Password.IsNullOrEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(login.Password));
|
|
}
|
|
}
|
|
|
|
protected virtual async Task CheckLocalLoginAsync()
|
|
{
|
|
if (!await SettingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin))
|
|
{
|
|
throw new UserFriendlyException(L["LocalLoginDisabledMessage"]);
|
|
}
|
|
}
|
|
}
|
|
}
|