add in cookie in now

Co-authored-by: and logout too
This commit is contained in:
2026-08-14 17:59:47 +10:00
co-authored by and logout too
parent 70083f1230
commit 4eafa2fa8d
@@ -37,8 +37,25 @@ public class UsersController : ControllerBase
public async Task<IActionResult> LoginApiAD([FromBody] AuthenticateRequest model) public async Task<IActionResult> LoginApiAD([FromBody] AuthenticateRequest model)
{ {
string remoteIpAddress = HttpContext.Connection.RemoteIpAddress?.ToString(); string remoteIpAddress = HttpContext.Connection.RemoteIpAddress?.ToString();
var response = await _userService.LoginApiAD(model, remoteIpAddress); var rval = await _repoUser.LoginApiAD(userdetail, remoteIpAddress);
return Ok(response); if (rval.Data != null && rval.Data.Token != null)
{
// Configure cookie options
var cookieOptions = new CookieOptions
{
HttpOnly = true, // Blocks client-side JavaScript reading/theft
Secure = false, // Requires HTTPS connections
SameSite = SameSiteMode.Unspecified, // Prevents CSRF attacks
Expires = DateTimeOffset.UtcNow.AddDays(1), // Match your token lifespan
Path = "/" // Accessible across entire domain structure
};
// Write cookie into response headers
Response.Cookies.Append("token", rval.Data.Token, cookieOptions);
}
return Ok(rval);
} }
[HttpPost("[action]")] [HttpPost("[action]")]
@@ -51,6 +68,14 @@ public class UsersController : ControllerBase
string token = Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last(); string token = Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
string remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString(); string remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();
var response = await _userService.Logout(token, currentUser, remoteIpAddress); var response = await _userService.Logout(token, currentUser, remoteIpAddress);
Response.Cookies.Delete("token", new CookieOptions
{
HttpOnly = true,
Secure = false,
SameSite = SameSiteMode.Unspecified,
Path = "/"
});
return Ok(response); return Ok(response);
} }