Cookies in ASP vnext

How can I use cookies in ASP MVC 6? I want to set and read cookie variables.

The HttpCookie class cannot be resolved.

Only the next line works, but I could not find a way to read the cookie after adding it. Response.Cookies.Append ("test", "test");

+6
source share
2 answers

See how cookies are used in the official MusicStore sample: https://github.com/aspnet/MusicStore/blob/a7ba4f8ffe5ed23b2a2d55e8e1226e64066a7ada/src/MusicStore/Models/ShoppingCart.cs#L152

public string GetCartId(HttpContext context) { var sessionCookie = context.Request.Cookies.Get("Session"); 
+12
source

The answer from Victor Khurdugachi relates to the preliminary releases of RC2, and this has changed a bit, so here is the current (and, hopefully, final) stage:

You set a cookie in response to:

 HttpContext.Response.Cookies.Append("key", "value"); 

Here the cookie is IResponseCookies . You can write only on it.

Then it will be sent to the browser.

You can read the cookies sent by the browser in the Request object:

 HttpContext.Request.Cookies["key"] 

Cookies here is an IRequestCookieCollection , so you can also read it.

+7
source

Source: https://habr.com/ru/post/983830/


All Articles