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

Cookies & Session in ASP.NET MVC

Introduction

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

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

Retrieve Cookies information

Session

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.

Get session value into view from the following syntax.

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.

Share:
Category:MVC
PREVIOUS POST
Bundling and Minification in ASP.NET MVC
NEXT POST
Areas in MVC