|
|
|
|
@ -0,0 +1,103 @@
|
|
|
|
|
using System;
|
|
|
|
|
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 UserLoginInfo = Volo.Abp.Account.Web.Areas.Account.Controllers.Models.UserLoginInfo;
|
|
|
|
|
|
|
|
|
|
namespace Volo.Abp.Account.Web.Areas.Account.Controllers
|
|
|
|
|
{
|
|
|
|
|
[RemoteService]
|
|
|
|
|
[Controller]
|
|
|
|
|
[ControllerName("Login")]
|
|
|
|
|
[Area("Account")]
|
|
|
|
|
[Route("api/account/login")]
|
|
|
|
|
public class LoginController : AbpController
|
|
|
|
|
{
|
|
|
|
|
private readonly SignInManager<IdentityUser> _signInManager;
|
|
|
|
|
public IStringLocalizer<AccountResource> L { get; set; }
|
|
|
|
|
public string AspNetCoreIdentityCookieName = ".AspNetCore." + IdentityConstants.ApplicationScheme;
|
|
|
|
|
|
|
|
|
|
public LoginController(SignInManager<IdentityUser> signInManager)
|
|
|
|
|
{
|
|
|
|
|
_signInManager = signInManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("")]
|
|
|
|
|
public virtual async Task<IActionResult> Login(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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = await _signInManager.PasswordSignInAsync(
|
|
|
|
|
login.UserNameOrEmailAddress,
|
|
|
|
|
login.Password,
|
|
|
|
|
login.RememberMe,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (result.IsLockedOut)
|
|
|
|
|
{
|
|
|
|
|
return Json(new AbpLoginResult(LoginResultType.LockedOut));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.RequiresTwoFactor)
|
|
|
|
|
{
|
|
|
|
|
return Json(new AbpLoginResult(LoginResultType.RequiresTwoFactor));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.IsNotAllowed)
|
|
|
|
|
{
|
|
|
|
|
return Json(new AbpLoginResult(LoginResultType.NotAllowed));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
return Json(new AbpLoginResult(LoginResultType.InvalidUserNameOrPassword));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Json(new AbpLoginResult(LoginResultType.Success)
|
|
|
|
|
{
|
|
|
|
|
IdentityCookieToken = GetCookieValueFromResponse(AspNetCoreIdentityCookieName) //todo: cookie name can be retrieved from UseAuthentication options
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetCookieValueFromResponse(string cookieName)
|
|
|
|
|
{
|
|
|
|
|
foreach (var headers in Response.Headers.Values)
|
|
|
|
|
{
|
|
|
|
|
foreach (var header in headers)
|
|
|
|
|
{
|
|
|
|
|
if (!header.StartsWith($"{cookieName}="))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var p1 = header.IndexOf('=');
|
|
|
|
|
var p2 = header.IndexOf(';');
|
|
|
|
|
return header.Substring(p1 + 1, p2 - p1 - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|