Cookie is often used to save a piece of data related to the user and saved on the client computer. In most browsers, each cookie will exist in the form of a small file, except for Firefox, which saves all cookies in a file. Among them, the cookie is expressed in the form of key-value, which can be used to read, delete, write and other operations
ASP.NET Core uses the cookie mechanism to maintain the session state. In every request from the client, the server will put the sessionid in the cookie and send it to the client for user tracking.
Read cookie
- To read Cookie ASP.NET Core can be used in the
Request.Cookies
property, cookie read from the Request object
string cookie = Request.Cookies["Key"];
- If you want to set a cookie expiration time, you can use
Cookies.Append
overloaded methods, as shown in the following code:
CookieOptions option = new CookieOptions();
option.Expires = DateTime.Now.AddMilliseconds(10);
Response.Cookies.Append(key, value, option);
As you can see from the code above, CookieOptions can be used to configure some additional parameters during the cookie generation process
parameter | Description |
---|---|
Domain | Used to specify the domain level associated with the cookie |
Expiration time | Used to specify the expiration time of the cookie |
Path | Used to specify the path of the cookie |
Security policy | Used to specify whether the cookie can be accessed through Https |
HttpOnly | Used to specify whether the cookie is available on the server side |
Write cookie
- To write cookie, you can use
Cookies.Append
the method
Response.Cookies.Append(somekey, somevalue);
Delete cookie
- To delete the cookie, you can use
Cookies.Delete
the method
Response.Cookies.Delete(somekey);
Use HttpContext
How to use the ASP.NET Core cookie, cookie must have in order to get
Request
the object, in order to obtain the Request object must haveHttpContext
, in order to getHttpContext
necessary to take advantage ofIHttpContextAccessor
the interface
public interface IHttpContextAccessor
{
HttpContext HttpContext {
get; set; }
}
- ASP.NET Core on a built-in implements the interface
HttpContextAccessor
class
public class HttpContextAccessor : IHttpContextAccessor
{
public HttpContextAccessor();
public HttpContext HttpContext {
get; set; }
}
Some people may ask, why not just use the parent class here Controller 's
Request
andHttpContext
this is because this approach is a strong dependence, then everyone shows how to get dependency injection by the wayHttpContext
, this is more flexible.
In order to achieve dependency injection, necessary
IHttpContextAccessor
andHttpContextAccessor
injected intoServiceCollection
, the following cases will be a single injection mode
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor,
HttpContextAccessor>();
// 逻辑代码
}
Can then be obtained by injection-dependent manner
IHttpContextAccessor
instance of an interface, and then sequentially acquiringHttpContext
objects, the following code fragment shows how to access the ControllerIHttpContextAccessor
example
public class HomeController : Controller
{
private readonly IHttpContextAccessor _httpContextAccessor;
public HomeController(IHttpContextAccessor httpContextAccessor)
{
this._httpContextAccessor = httpContextAccessor;
}
// 逻辑代码
}
Write cookie in Controller
- Write the data to the cookie in the Controller by the following method
public IActionResult Write(string key, string value, bool isPersistent)
{
CookieOptions options = new CookieOptions();
if (isPersistent)
options.Expires = DateTime.Now.AddDays(1);
else
options.Expires = DateTime.Now.AddSeconds(10);
_httpContextAccessor.HttpContext.Response.Cookies.Append(key, value, options);
return View("WriteCookie");
}
Read cookie in Controller
- Once the cookie is successfully written, you can use the following method to read the cookie
public IActionResult Read(string key)
{
ViewBag.Data = _httpContextAccessor.HttpContext.Request.Cookies[key];
return View("ReadCookie");
}
- You can use the browser's developer tools whether to view Cookie successfully written and read to
====