2022-09-10 00:33:17 +03:00
|
|
|
using System.Threading.Tasks;
|
2022-09-27 20:12:13 +03:00
|
|
|
using BlueWest.Data.Auth;
|
|
|
|
using BlueWest.Data.Auth.Context.Users;
|
2022-09-10 00:33:17 +03:00
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace BlueWest.WebApi.Controllers;
|
|
|
|
|
2022-09-10 07:12:03 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Auth controller
|
|
|
|
/// </summary>
|
2022-09-10 00:33:17 +03:00
|
|
|
[ApiController]
|
2022-09-17 22:13:35 +03:00
|
|
|
[Route("api/[controller]")]
|
2022-09-18 04:00:24 +03:00
|
|
|
[Authorize(Policy = SessionConstants.ApiNamePolicy)]
|
2022-09-17 22:13:35 +03:00
|
|
|
|
|
|
|
/*[EnableCors(Constants.CorsPolicyName)]*/
|
|
|
|
public class AuthController : Controller
|
2022-09-10 00:33:17 +03:00
|
|
|
{
|
|
|
|
private readonly IAuthManager _authManager;
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
2022-09-10 07:12:03 +03:00
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="authManager"></param>
|
|
|
|
/// <param name="userManager"></param>
|
2022-09-18 04:00:24 +03:00
|
|
|
public AuthController( IAuthManager authManager, IUserManager userManager)
|
2022-09-10 00:33:17 +03:00
|
|
|
{
|
|
|
|
_authManager = authManager;
|
|
|
|
_userManager = userManager;
|
|
|
|
}
|
2022-09-10 07:12:03 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Signup user
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="registerViewModel"></param>
|
|
|
|
/// <returns></returns>
|
2022-09-10 00:33:17 +03:00
|
|
|
[AllowAnonymous]
|
|
|
|
[HttpPost("register")]
|
|
|
|
public async Task<ActionResult<IdentityResult>> SignupUserAsync(RegisterViewModel registerViewModel)
|
|
|
|
{
|
|
|
|
return await _authManager.CreateUserAsync(registerViewModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-17 22:13:35 +03:00
|
|
|
|
2022-09-11 01:22:04 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a bearer token
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="loginViewModel"></param>
|
|
|
|
/// <returns></returns>
|
2022-09-10 00:33:17 +03:00
|
|
|
[AllowAnonymous]
|
2022-09-18 04:00:24 +03:00
|
|
|
[HttpPost("login")]
|
|
|
|
public async Task<ActionResult<IdentityResult>> GetSessionToken(LoginRequest loginViewModel)
|
2022-09-10 00:33:17 +03:00
|
|
|
{
|
2022-09-19 05:50:15 +03:00
|
|
|
var (success, sessionToken, _) = await _authManager.GetSessionTokenIdByLoginRequest(loginViewModel);
|
2022-09-10 00:33:17 +03:00
|
|
|
|
2022-09-17 22:13:35 +03:00
|
|
|
if (success)
|
2022-09-10 00:33:17 +03:00
|
|
|
{
|
2022-09-18 04:00:24 +03:00
|
|
|
return Ok(new {sessionToken});
|
2022-09-10 00:33:17 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
return Problem();
|
|
|
|
}
|
2022-09-17 22:13:35 +03:00
|
|
|
|
2022-09-10 00:33:17 +03:00
|
|
|
|
2022-09-11 01:22:04 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Do Cookie based login.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="loginDto"></param>
|
|
|
|
/// <returns></returns>
|
2022-09-18 04:00:24 +03:00
|
|
|
/*[AllowAnonymous]
|
2022-09-17 22:13:35 +03:00
|
|
|
[HttpPost("login")]
|
2022-09-18 04:00:24 +03:00
|
|
|
public async Task<ActionResult> DoLoginByCookie(LoginRequest loginDto)
|
2022-09-10 00:33:17 +03:00
|
|
|
{
|
2022-09-18 04:00:24 +03:00
|
|
|
var (success, sessionToken, identity) = await _authManager.GetSessionTokenId(loginDto);
|
2022-09-17 22:13:35 +03:00
|
|
|
|
|
|
|
if (success)
|
2022-09-10 00:33:17 +03:00
|
|
|
{
|
2022-09-17 22:13:35 +03:00
|
|
|
await HttpContext.SignInAsync(
|
|
|
|
CookieAuthenticationDefaults.AuthenticationScheme,
|
|
|
|
new ClaimsPrincipal(identity),
|
|
|
|
new AuthenticationProperties
|
|
|
|
{
|
|
|
|
IsPersistent = true,
|
2022-09-18 04:00:24 +03:00
|
|
|
ExpiresUtc = DateTime.UtcNow.Add(SessionConstants.DefaultValidForSpan)
|
2022-09-17 22:13:35 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return Ok(new {authenticated = true, sessionToken});
|
2022-09-10 00:33:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-17 22:13:35 +03:00
|
|
|
return new ForbidResult(CookieAuthenticationDefaults.AuthenticationScheme);
|
2022-09-18 04:00:24 +03:00
|
|
|
}*/
|
2022-09-10 00:33:17 +03:00
|
|
|
|
2022-09-11 01:22:04 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Do Cookie based logout
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
2022-09-10 00:33:17 +03:00
|
|
|
[AllowAnonymous]
|
|
|
|
[HttpPost("logout")]
|
2022-09-18 04:00:24 +03:00
|
|
|
public async Task DoCookieLogoutAsync()
|
2022-09-10 00:33:17 +03:00
|
|
|
{
|
2022-09-17 22:13:35 +03:00
|
|
|
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
2022-09-10 00:33:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|