15 49.0138 8.38624 1 0 4000 1 https://codeisall.com 300
cookies and session in MVC

Cookies & Session in ASP.NET MVC


In theĀ previous article, you have learned about what is Bundling and Minification in MVC application.

In this article, you will learn about what is Cookies and Session and how to use this in an MVC application.

This article will be helpful to beginners and students who are learning .


Cookies are used to store the information. It stored only at the client-side machine. Basically Cookies are a text file stored at a client machine for the tracking purpose.

The server sends a set of cookies to the browser. For example age, name or gender, identification number, etc. And browser saves this information on a local machine for future use.

So, when the browser sends a request to the server, it sends the cookies along with the request, so that server can identify which user sent this request.

Cookie store in client-side storage of data variables. Different users at the same machine can read the same cookie. Because of this :

  • You should not store sensitive data on cookies.
  • You should not store data that belongs to one user account.
  • Cookie has no effect on server resources.
  • The cookie expires at the specified date by mention in expire time.

Properties of HttpCookies Class

  • Name: Contains the name of the Key.
  • Domain: Using these properties we can set the domain of the cookie.
  • Expires: This property sets the Expiration time of the cookies.
  • HasKeys: If the cookies have a subkey then it returns True.
  • Value: Contains the value of the cookies.
  • Secured:If the cookies are to be passed in a secure connection then it only returns True.
  • Path: Contains the Virtual Path to be submitted with the Cookies.

Add Information into cokkies

public ActionResult cookies()
    // Create the cookie object.
    HttpCookie cookie = new HttpCookie("CodeIsAll");
    cookie["website"] = "codeisall.com"; 
    // This cookie will remain  for one month.
    cookie.Expires = DateTime.Now.AddMonths(1);
    // Add it to the current web response.
    return View();

Retrieve Cookies information

HttpCookie cookieObj = Request.Cookies["CodeIsAll"];
string _websiteValue = cookieObj["website"];


Session is a temporary storage, It is used to store and retrive the value for a perticular time session.

Session is generally used to store the information temporarily, like when user login we can save the basic information temporary, so when we need them we don’t have to do a database calls every time.

Another example of session is, suppose you have one system where you login and you want a user to login after every 4 hours, then you can expire session to make them login again.

Each created session is stored in SessionStateItemCollection object.

public ActionResult session()
    // Create the session.
    Session["website"] = "codeisall.com"; 
    return View();

Get session value into view from the following syntax.

@if(Session["website"]!=null) {

session value : @Session["website"] }

I hope this article will help you to understand how cookies and session can be used in MVC application and also how beneficial they are to use. we will be happy to help you if you have any issues or queries. Comment your suggestions and issues and we will try to resolve it.

Bundling and Minification in ASP.NET MVC
Areas in MVC