2019-02-15 23:19:59 -05:00
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2018-04-04 15:38:34 -04:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
2019-08-04 21:38:55 -04:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2019-02-15 23:19:59 -05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2019-03-31 20:56:31 -04:00
|
|
|
|
using SharedLibraryCore;
|
2018-04-05 00:38:45 -04:00
|
|
|
|
using System;
|
2019-02-15 23:19:59 -05:00
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-04-04 15:38:34 -04:00
|
|
|
|
|
|
|
|
|
namespace WebfrontCore.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class AccountController : BaseController
|
|
|
|
|
{
|
2019-02-22 20:06:51 -05:00
|
|
|
|
|
2018-04-04 15:38:34 -04:00
|
|
|
|
[HttpGet]
|
2019-08-04 21:38:55 -04:00
|
|
|
|
public async Task<IActionResult> LoginAsync(int clientId, string password)
|
2018-04-04 15:38:34 -04:00
|
|
|
|
{
|
2018-04-05 18:50:04 -04:00
|
|
|
|
if (clientId == 0 || string.IsNullOrEmpty(password))
|
2018-04-04 15:38:34 -04:00
|
|
|
|
{
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 00:38:45 -04:00
|
|
|
|
try
|
2018-04-04 15:38:34 -04:00
|
|
|
|
{
|
2019-08-02 19:04:34 -04:00
|
|
|
|
var privilegedClient = await Manager.GetClientService().GetClientForLogin(clientId);
|
2019-07-29 13:08:25 -04:00
|
|
|
|
bool loginSuccess = false;
|
|
|
|
|
#if DEBUG
|
|
|
|
|
loginSuccess = clientId == 1;
|
|
|
|
|
#endif
|
|
|
|
|
if (!Authorized && !loginSuccess)
|
|
|
|
|
{
|
|
|
|
|
loginSuccess = Manager.TokenAuthenticator.AuthorizeToken(privilegedClient.NetworkId, password) ||
|
|
|
|
|
(await Task.FromResult(SharedLibraryCore.Helpers.Hashing.Hash(password, privilegedClient.PasswordSalt)))[0] == privilegedClient.Password;
|
|
|
|
|
}
|
2019-02-16 16:04:40 -05:00
|
|
|
|
|
2019-02-22 20:06:51 -05:00
|
|
|
|
if (loginSuccess)
|
2018-04-04 15:38:34 -04:00
|
|
|
|
{
|
2018-04-05 00:38:45 -04:00
|
|
|
|
var claims = new[]
|
|
|
|
|
{
|
2019-04-17 18:50:53 -04:00
|
|
|
|
new Claim(ClaimTypes.NameIdentifier, privilegedClient.Name),
|
|
|
|
|
new Claim(ClaimTypes.Role, privilegedClient.Level.ToString()),
|
|
|
|
|
new Claim(ClaimTypes.Sid, privilegedClient.ClientId.ToString()),
|
|
|
|
|
new Claim(ClaimTypes.PrimarySid, privilegedClient.NetworkId.ToString("X"))
|
2018-04-07 17:47:21 -04:00
|
|
|
|
};
|
2018-04-04 15:38:34 -04:00
|
|
|
|
|
2018-04-05 00:38:45 -04:00
|
|
|
|
var claimsIdentity = new ClaimsIdentity(claims, "login");
|
|
|
|
|
var claimsPrinciple = new ClaimsPrincipal(claimsIdentity);
|
2019-08-04 21:38:55 -04:00
|
|
|
|
await SignInAsync(claimsPrinciple);
|
2018-04-05 00:38:45 -04:00
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-04 15:38:34 -04:00
|
|
|
|
|
2018-04-05 00:38:45 -04:00
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return Unauthorized();
|
2018-04-04 15:38:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
2018-04-05 18:50:04 -04:00
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> LogoutAsync()
|
|
|
|
|
{
|
2018-04-08 02:44:42 -04:00
|
|
|
|
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
2018-04-05 18:50:04 -04:00
|
|
|
|
return RedirectToAction("Index", "Home");
|
|
|
|
|
}
|
2018-04-04 15:38:34 -04:00
|
|
|
|
}
|
|
|
|
|
}
|