IW4M-Admin/WebfrontCore/Controllers/AccountController.cs

64 lines
2.1 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.Cookies;
using System.Security.Claims;
using System;
2018-04-08 02:44:42 -04:00
using Microsoft.AspNetCore.Authentication;
namespace WebfrontCore.Controllers
{
public class AccountController : BaseController
{
[HttpGet]
public async Task<IActionResult> LoginAsync(int clientId, string password)
{
if (clientId == 0 || string.IsNullOrEmpty(password))
{
return Unauthorized();
}
try
{
var client = Manager.GetPrivilegedClients()[clientId];
2018-04-08 02:44:42 -04:00
string[] hashedPassword = await Task.FromResult(SharedLibraryCore.Helpers.Hashing.Hash(password, client.PasswordSalt));
if (hashedPassword[0] == client.Password)
{
var claims = new[]
{
2018-04-07 17:47:21 -04:00
new Claim(ClaimTypes.NameIdentifier, client.Name),
new Claim(ClaimTypes.Role, client.Level.ToString()),
new Claim(ClaimTypes.Sid, client.ClientId.ToString())
};
var claimsIdentity = new ClaimsIdentity(claims, "login");
var claimsPrinciple = new ClaimsPrincipal(claimsIdentity);
2018-04-08 02:44:42 -04:00
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrinciple, new AuthenticationProperties()
{
AllowRefresh = true,
ExpiresUtc = DateTime.UtcNow.AddDays(30),
IsPersistent = true,
IssuedUtc = DateTime.UtcNow
});
return Ok();
}
}
catch (Exception)
{
return Unauthorized();
}
return Unauthorized();
}
[HttpGet]
public async Task<IActionResult> LogoutAsync()
{
2018-04-08 02:44:42 -04:00
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return RedirectToAction("Index", "Home");
}
}
}