2018-04-04 15:38:34 -04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
|
using System.Security.Claims;
|
2018-04-05 00:38:45 -04:00
|
|
|
|
using System;
|
2018-04-04 15:38:34 -04:00
|
|
|
|
|
|
|
|
|
namespace WebfrontCore.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class AccountController : BaseController
|
|
|
|
|
{
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> Login(int userId, string password)
|
|
|
|
|
{
|
|
|
|
|
if (userId == 0 || string.IsNullOrEmpty(password))
|
|
|
|
|
{
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 00:38:45 -04:00
|
|
|
|
try
|
2018-04-04 15:38:34 -04:00
|
|
|
|
{
|
2018-04-05 00:38:45 -04:00
|
|
|
|
var client = IW4MAdmin.Program.ServerManager.PrivilegedClients[userId];
|
|
|
|
|
string[] hashedPassword = await Task.FromResult(SharedLibrary.Helpers.Hashing.Hash(password, client.PasswordSalt));
|
|
|
|
|
|
|
|
|
|
if (hashedPassword[0] == client.Password)
|
2018-04-04 15:38:34 -04:00
|
|
|
|
{
|
2018-04-05 00:38:45 -04:00
|
|
|
|
var claims = new[]
|
|
|
|
|
{
|
2018-04-04 15:38:34 -04:00
|
|
|
|
new Claim(ClaimTypes.NameIdentifier, client.Name),
|
|
|
|
|
new Claim(ClaimTypes.Role, client.Level.ToString()),
|
|
|
|
|
new Claim(ClaimTypes.Sid, client.ClientId.ToString())
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-05 00:38:45 -04:00
|
|
|
|
var claimsIdentity = new ClaimsIdentity(claims, "login");
|
|
|
|
|
var claimsPrinciple = new ClaimsPrincipal(claimsIdentity);
|
|
|
|
|
await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrinciple, new Microsoft.AspNetCore.Http.Authentication.AuthenticationProperties()
|
|
|
|
|
{
|
|
|
|
|
AllowRefresh = true,
|
|
|
|
|
ExpiresUtc = DateTime.UtcNow.AddDays(30),
|
|
|
|
|
IsPersistent = true,
|
|
|
|
|
IssuedUtc = DateTime.UtcNow
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|