[Solved]-Cookie Loses Value When Page Is Changed In MVC


I'm working on a web application where I need to implement the "remember me" feature. To achieve this, I decided to create a cookie to store the user's username.

However, I encountered an issue. After a user logs in to the web application, I create a cookie to store their username. But when the user navigates from the login page to another page, the cookie loses its value. Even though the cookie is still present, its value becomes empty. This behavior is unexpected and I'm unable to retrieve the cookie value in another action method.

After double-checking my code and debugging to ensure that the cookie isn't being rewritten, I spent about 1-2 hours searching on Google to find a solution to the issue. Eventually, I found the solution that worked for me. Therefore, I decided to write down the solution that resolved the problem.

Point in the Post:

  • ASP.NET MVC Cookie gets deleted after redirect to action
  • Response.Cookies gets reset when RedirectToAction is called
  • Implement Remember Me with Login in ASP.Net MVC

    We just need to set the cookie. Secure property to false.

 HttpCookie _rememberme = new HttpCookie("_rememberme");
                    _rememberme.Expires = DateTime.Now.AddDays(30);
                    _rememberme["UserName"] = ac.UserName;
                    _rememberme["Password"] = ac.Password;
                    _rememberme.Secure = false; //this line of code is responsible for that
                    Response.Cookies.Add(_rememberme);

 

C# code  used in web development to set a cookie named "_rememberme" with certain properties.

HttpCookie _rememberme = new HttpCookie("_rememberme");: This line of code creates a new instance of the HttpCookie class named "_rememberme".

_rememberme.Expires = DateTime.Now.AddDays(30);: This code sets the expiration date of the cookie to 30 days from the current date and time. This means the cookie will remain valid for 30 days.

_rememberme["UserName"] = ac.UserName;: Here we are assigning the value of the "UserName" property of some object ac to the cookie with the key "UserName". This is a way to store the user's username in the cookie.

_rememberme["Password"] = ac.Password;: Assigning value of the "Password" property of the ac object to the cookie with the key "Password". However, it's important to note that storing passwords in cookies is generally not recommended due to security reasons. 

_rememberme.Secure = false;: Sets the Secure property of the cookie to false. This means that the cookie can be transmitted over non-secure (HTTP) connections. Setting this to true would mean the cookie is only transmitted over secure (HTTPS) connections.

Response.Cookies.Add(_rememberme);: adds the _rememberme cookie to the collection of cookies in the HTTP response, which will be sent to the client's browser. This is how the cookie is actually set and sent to the client's browser.

There are many types of files in our mobiles and laptops, out of which cookies are also a normal text file present in our hard drive which is automatically saved from the Internet.

Whenever we open a website, all its information is saved in a file so that whenever we open a website again, it is easier to open any page related to it, and thus we also take less time. And the data cost is also less.

If we talk technically, it works like a kind of identification card which is very important for advertising size and e-commerce websites on the internet because it is very effective to show all types of ads related to them.

Just as the names of their operating systems are placed on food items in Android mobiles, similarly the name of Internet Cookies is associated with a food item. So today we will tell you in this article what is cookies and how does it work and what are its benefits.