2019-02-15 22:19:59 -06:00
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2018-04-04 14:38:34 -05:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
2019-02-15 22:19:59 -06:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2018-04-04 23:38:45 -05:00
|
|
|
|
using System;
|
2019-02-15 22:19:59 -06:00
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-04-04 14:38:34 -05:00
|
|
|
|
|
|
|
|
|
namespace WebfrontCore.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class AccountController : BaseController
|
|
|
|
|
{
|
2019-02-22 19:06:51 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// life span in months
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const int COOKIE_LIFESPAN = 3;
|
|
|
|
|
|
2018-04-04 14:38:34 -05:00
|
|
|
|
[HttpGet]
|
2018-04-05 17:50:04 -05:00
|
|
|
|
public async Task<IActionResult> LoginAsync(int clientId, string password)
|
2018-04-04 14:38:34 -05:00
|
|
|
|
{
|
2018-04-05 17:50:04 -05:00
|
|
|
|
if (clientId == 0 || string.IsNullOrEmpty(password))
|
2018-04-04 14:38:34 -05:00
|
|
|
|
{
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-04 23:38:45 -05:00
|
|
|
|
try
|
2018-04-04 14:38:34 -05:00
|
|
|
|
{
|
2018-04-08 13:48:40 -05:00
|
|
|
|
var client = Manager.GetPrivilegedClients()[clientId];
|
2018-04-04 23:38:45 -05:00
|
|
|
|
|
2019-02-22 19:06:51 -06:00
|
|
|
|
bool loginSuccess = Manager.TokenAuthenticator.AuthorizeToken(client.NetworkId, password) ||
|
|
|
|
|
(await Task.FromResult(SharedLibraryCore.Helpers.Hashing.Hash(password, client.PasswordSalt)))[0] == client.Password;
|
2019-02-16 15:04:40 -06:00
|
|
|
|
|
2019-02-22 19:06:51 -06:00
|
|
|
|
if (loginSuccess)
|
2018-04-04 14:38:34 -05:00
|
|
|
|
{
|
2018-04-04 23:38:45 -05:00
|
|
|
|
var claims = new[]
|
|
|
|
|
{
|
2018-04-07 16:47:21 -05:00
|
|
|
|
new Claim(ClaimTypes.NameIdentifier, client.Name),
|
|
|
|
|
new Claim(ClaimTypes.Role, client.Level.ToString()),
|
2019-01-03 14:39:22 -06:00
|
|
|
|
new Claim(ClaimTypes.Sid, client.ClientId.ToString()),
|
|
|
|
|
new Claim(ClaimTypes.PrimarySid, client.NetworkId.ToString())
|
2018-04-07 16:47:21 -05:00
|
|
|
|
};
|
2018-04-04 14:38:34 -05:00
|
|
|
|
|
2018-04-04 23:38:45 -05:00
|
|
|
|
var claimsIdentity = new ClaimsIdentity(claims, "login");
|
|
|
|
|
var claimsPrinciple = new ClaimsPrincipal(claimsIdentity);
|
2018-04-08 01:44:42 -05:00
|
|
|
|
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrinciple, new AuthenticationProperties()
|
2018-04-04 23:38:45 -05:00
|
|
|
|
{
|
|
|
|
|
AllowRefresh = true,
|
2019-02-22 19:06:51 -06:00
|
|
|
|
ExpiresUtc = DateTime.UtcNow.AddMonths(COOKIE_LIFESPAN),
|
2018-04-04 23:38:45 -05:00
|
|
|
|
IsPersistent = true,
|
|
|
|
|
IssuedUtc = DateTime.UtcNow
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-04 14:38:34 -05:00
|
|
|
|
|
2018-04-04 23:38:45 -05:00
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return Unauthorized();
|
2018-04-04 14:38:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
2018-04-05 17:50:04 -05:00
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> LogoutAsync()
|
|
|
|
|
{
|
2018-04-08 01:44:42 -05:00
|
|
|
|
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
2018-04-05 17:50:04 -05:00
|
|
|
|
return RedirectToAction("Index", "Home");
|
|
|
|
|
}
|
2018-04-04 14:38:34 -05:00
|
|
|
|
}
|
|
|
|
|
}
|