Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cookies in ASP vnext

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

HttpCookie class can't be resolved .

Only the following line works but i couldn't find a way to read the cookie after adding it . Response.Cookies.Append("test", "test");

like image 448
F Andrei Avatar asked Mar 15 '15 18:03

F Andrei


People also ask

What is ASP NET Cookie?

ASP.NET Cookie is a small bit of text that is used to store user-specific information. This information can be read by the web application whenever user visits the site.

What is Cookieless session in ASP NET?

By default, ASP.NET uses a non-persistent cookie to store the session state. However, if a user has disabled cookies on the browser, session state information cannot be stored in a cookie. ASP.NET offers an alternative in the form of cookieless sessions. You can configure your application to store session IDs not in a cookie, but in the URLs of ...

How do I create a httpcookie?

The System.Web namespace offers a class called HttpCookie to create cookies. The cookies are sent to the Browser via the HttpResponse object that exposes a collection called cookies. You can access the HttpResponse object as the Response property of your page class.

How to read cookies from httpresponse object in ASP NET?

You can access the HttpResponse object as the Response property of your page class. In this ASP.NET application you can read the cookies using the HttpRequest object which is available as the Request property of the page class. We added one Textbox two Buttons and a Label to the web page.


1 Answers

Take a look at 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"); 
like image 99
Victor Hurdugaci Avatar answered Oct 09 '22 00:10

Victor Hurdugaci