Add and retrieve cookies using C# in ASP.Net MVC

Last Reply on Feb 26, 2018 11:00 PM By mukesh1

Posted on Feb 26, 2018 04:13 AM

Want to add id of user in cookies when user first login, and cookies will remove after 320 days.

Mainly i am working in mvc using entity framework with database first approach.

You are viewing reply posted by: mukesh1 on Feb 26, 2018 11:00 PM.
Posted on Feb 26, 2018 11:00 PM

 

HttpCookie myCookie = new HttpCookie("ID"); 
myCookie["ID"] = Aunthicate.ID.ToString(); 
//myCookie.Expires = DateTime.Now.AddDays(-1d); 
System.Web.HttpContext.Current.Response.Cookies.Add(myCookie);

 and for get cookies value in session

Session["ID"] = Server.HtmlEncode(Request.Cookies["ID"].Value);