|
|
|
@ -1,12 +1,12 @@ |
|
|
|
using System; |
|
|
|
using System.Linq; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Microsoft.AspNetCore.Identity; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using Microsoft.Extensions.Localization; |
|
|
|
using Volo.Abp.Account.Web.Areas.Account.Controllers.Models; |
|
|
|
using Volo.Abp.Account.Web.Localization; |
|
|
|
using Volo.Abp.AspNetCore.Mvc; |
|
|
|
using Volo.Abp.Identity; |
|
|
|
using SignInResult = Microsoft.AspNetCore.Identity.SignInResult; |
|
|
|
using UserLoginInfo = Volo.Abp.Account.Web.Areas.Account.Controllers.Models.UserLoginInfo; |
|
|
|
|
|
|
|
namespace Volo.Abp.Account.Web.Areas.Account.Controllers |
|
|
|
@ -15,44 +15,53 @@ namespace Volo.Abp.Account.Web.Areas.Account.Controllers |
|
|
|
[Controller] |
|
|
|
[ControllerName("Login")] |
|
|
|
[Area("Account")] |
|
|
|
[Route("api/account/login")] |
|
|
|
public class LoginController : AbpController |
|
|
|
[Route("api/account")] |
|
|
|
public class AccountController : AbpController |
|
|
|
{ |
|
|
|
private readonly SignInManager<IdentityUser> _signInManager; |
|
|
|
public IStringLocalizer<AccountResource> L { get; set; } |
|
|
|
public string AspNetCoreIdentityCookieName = ".AspNetCore." + IdentityConstants.ApplicationScheme; |
|
|
|
private readonly IdentityUserManager _userManager; |
|
|
|
|
|
|
|
public LoginController(SignInManager<IdentityUser> signInManager) |
|
|
|
public AccountController(SignInManager<IdentityUser> signInManager, IdentityUserManager userManager) |
|
|
|
{ |
|
|
|
_signInManager = signInManager; |
|
|
|
_userManager = userManager; |
|
|
|
} |
|
|
|
|
|
|
|
[HttpPost] |
|
|
|
[Route("")] |
|
|
|
[Route("login")] |
|
|
|
public virtual async Task<AbpLoginResult> Login(UserLoginInfo login) |
|
|
|
{ |
|
|
|
if (login == null) |
|
|
|
{ |
|
|
|
throw new ArgumentException(nameof(login)); |
|
|
|
} |
|
|
|
ValidateLoginInfo(login); |
|
|
|
|
|
|
|
if (login.UserNameOrEmailAddress.IsNullOrEmpty()) |
|
|
|
return GetAbpLoginResult(await _signInManager.PasswordSignInAsync( |
|
|
|
login.UserNameOrEmailAddress, |
|
|
|
login.Password, |
|
|
|
login.RememberMe, |
|
|
|
true |
|
|
|
)); |
|
|
|
} |
|
|
|
|
|
|
|
[HttpPost] |
|
|
|
[Route("checkPassword")] |
|
|
|
public virtual async Task<AbpLoginResult> CheckPassword(UserLoginInfo login) |
|
|
|
{ |
|
|
|
ValidateLoginInfo(login); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
throw new ArgumentNullException(nameof(login.UserNameOrEmailAddress)); |
|
|
|
IdentityUser identityUser = await _userManager.FindByNameAsync(login.UserNameOrEmailAddress); |
|
|
|
return GetAbpLoginResult(await _signInManager.CheckPasswordSignInAsync(identityUser, login.Password, true)); |
|
|
|
} |
|
|
|
|
|
|
|
if (login.Password.IsNullOrEmpty()) |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
throw new ArgumentNullException(nameof(login.Password)); |
|
|
|
Console.WriteLine(e); |
|
|
|
throw; |
|
|
|
} |
|
|
|
|
|
|
|
var result = await _signInManager.PasswordSignInAsync( |
|
|
|
login.UserNameOrEmailAddress, |
|
|
|
login.Password, |
|
|
|
login.RememberMe, |
|
|
|
true |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
private static AbpLoginResult GetAbpLoginResult(SignInResult result) |
|
|
|
{ |
|
|
|
if (result.IsLockedOut) |
|
|
|
{ |
|
|
|
return new AbpLoginResult(LoginResultType.LockedOut); |
|
|
|
@ -73,31 +82,25 @@ namespace Volo.Abp.Account.Web.Areas.Account.Controllers |
|
|
|
return new AbpLoginResult(LoginResultType.InvalidUserNameOrPassword); |
|
|
|
} |
|
|
|
|
|
|
|
return new AbpLoginResult(LoginResultType.Success) |
|
|
|
{ |
|
|
|
IdentityCookieToken = GetCookieValueFromResponse(AspNetCoreIdentityCookieName) |
|
|
|
}; |
|
|
|
return new AbpLoginResult(LoginResultType.Success); |
|
|
|
} |
|
|
|
|
|
|
|
private string GetCookieValueFromResponse(string cookieName) |
|
|
|
private void ValidateLoginInfo(UserLoginInfo login) |
|
|
|
{ |
|
|
|
foreach (var headers in Response.Headers.Values) |
|
|
|
if (login == null) |
|
|
|
{ |
|
|
|
foreach (var header in headers) |
|
|
|
{ |
|
|
|
if (!header.StartsWith($"{cookieName}=")) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
throw new ArgumentException(nameof(login)); |
|
|
|
} |
|
|
|
|
|
|
|
var p1 = header.IndexOf('='); |
|
|
|
var p2 = header.IndexOf(';'); |
|
|
|
return header.Substring(p1 + 1, p2 - p1 - 1); |
|
|
|
} |
|
|
|
if (login.UserNameOrEmailAddress.IsNullOrEmpty()) |
|
|
|
{ |
|
|
|
throw new ArgumentNullException(nameof(login.UserNameOrEmailAddress)); |
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
if (login.Password.IsNullOrEmpty()) |
|
|
|
{ |
|
|
|
throw new ArgumentNullException(nameof(login.Password)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |