In MVC 4 Save and Retrieve cookies

In MVC 4 How to save and retrieve cookies.

Follow these steps for Save Cookies :-

1. User validation successful then save UserID or UserName In cookies .In Login controller write these code:

Code:

var UserIDCookies = new HttpCookie("UserID",UserID); // Define cookies name
UserIDCookies.HtpOnly=true; // assign UserID for save in cookies
Response.Cookies.set(UserIDCookies);

// User ID set in cookies

2. When ever you want to Retrieve UserID from cookie.

Code:

var UserID - Request.cookies.Get("UserID").value;

In simple bracket of "UserID" name match with stored cookie name.
cookie value store in Variable called UserID.

You get UserID from cookies and store in variable.


You want to convert string to int in MVC

just write single line code.

int ID = Convert.ToInt32("UserID");

Comments

Popular posts from this blog

Angular 4 + Cordova Boilerplate ::: Hybrid Mobile Application