So I decided to create a simple web app which sends a login request to my login endpoint on my Web API like this.
await fetch("https://localhost:1234/api/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ Username: "Admin", Password: "Password" }),
})
.then((response) => response.json())
.then((jsondata) => {
console.log(jsondata);
});
}
And what happens on my Web API is this
public IActionResult Login([FromBody] UserLogin userLogin)
{
var user = Authenticate(userLogin);
if (user != null)
{
var token = Generate(user);
Response.Cookies.Append("Authorization", token, new CookieOptions { HttpOnly = true, Secure = true });
return Ok();
}
return NotFound("User not found.");
}
So when the user logs in with a valid username and password, it generates a JWT and adds it to the response cookies. When I make a request with Postman/Insomnia, I can see the cookie in the response which means that on the client, when I make the same request I should get the same response. However, the issue is that I have no idea how to retrieve that cookie and store it so that I can send it in later requests when trying to access pages that require a JWT to authorize.
When I check the browsers and I got to Application > Cookies
there is nothing there.
The goal is to be able to receive it, store is as a httpOnly cookie (because that's that's slightly safer than storing it on session/localStorage I've read) and then be able to use it to access other pages which requires me to send it to authorize.
That seems to be an issue because you can read or write httpOnly cookies using JavaScript https://stackoverflow.com/a/14691716/5693405
What's the proper way of dealing with this?