I am having difficulty to understand some code from GitHub (I am learning angular, however this is server side code written in c#)
The code is available on GitHub code).
I can't completely understand the very first line of code var refreshToken = Request.Cookies["refreshToken"];
Where does Request.Cookies
come from? It is not a variable and it looks like a static call to some array Cookies
. How does the element of that array happen to contain "refresh-token"
item?
Could someone please explain this? (this code comes from the class derived from BaseController
)
[HttpPost("refresh-token")]
public ActionResult<AuthenticateResponse> RefreshToken()
{
var refreshToken = Request.Cookies["refreshToken"];
var response = _accountService.RefreshToken(refreshToken, ipAddress());
setTokenCookie(response.RefreshToken);
return Ok(response);
}
CodePudding user response:
When you work in an HTTP application, .NET manages some context for you. A bunch of stuff you write, like your POST action, is provided with an HTTP context, which has properties that provide information about the request. This includes headers, cookies, etc.
When you use Request
within an MVC controller (or some other HTTP context) you'll get access to the HttpContext
and Request
that relates to the specific single request. It feels like magic, but it's the framework doing the work for you.
A bit more information on context.
CodePudding user response:
You need to check other serverside codes which set the cookie,Cookie is created in serverside firstly and sent to User Agent,often stored in your browser.next time you send a request,your request may contain the cookie
you could check the codes like:Response.Cookies.Append(....)