create cookie in asp.net mvc
create cookie in asp.net mvc
//assembly
using System.Web.Security;
-----------------------
//Web config
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
//Cookiie name and Time set
<forms cookieless="UseCookies" name="TGAUTHCookies" loginUrl="~/Login/Login" slidingExpiration="true" protection="All" timeout="1300" />
</authentication>
//save mode
<sessionState mode="InProc" cookieless="UseCookies" timeout="1300" />
</system.web>
-----------------
//Create Cookie
FormsAuthentication.SetAuthCookie("vijay", this.isExist);
HttpCookie TGAUTH = new HttpCookie("TGAUTH");
TGAUTH.Values.Add("userId", 2);
TGAUTH.Values.Add("firstName", "vijay");
//set the time Expire
TGAUTH.Expires = DateTime.Now.AddDays(2);
Response.Cookies.Add(TGAUTH);
------------------
//Clear Cookie
FormsAuthentication.SignOut();
//Expire day set -2
Response.Cookies["TGAUTH"].Expires = DateTime.Now.AddDays(-2);
Response.Cookies["TGAUTHCookies"].Expires = DateTime.Now.AddDays(-2);
//// Removes the cache
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetNoStore();
string[] cookies = Request.Cookies.AllKeys;
foreach (string cookie in cookies)
{
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
No comments:
Post a Comment