First of all, ASP.Net membership providers do not write any cookies; authentication cookies are written to FormsAuthentication.
And secondly, why bother with the authentication cookie? You can do this in a separate cookie. Here is how you can do it.
Writing value keys to a cookie.
//create a cookie HttpCookie myCookie = new HttpCookie("myCookie"); //Add key-values in the cookie myCookie.Values.Add("UserId", "your-UserId"); myCookie.Values.Add("UrlSlug", "your-UrlSlug"); //set cookie expiry date-time, if required. Made it to last for next 12 hours. myCookie.Expires = DateTime.Now.AddHours(12); //Most important, write the cookie to client. Response.Cookies.Add(myCookie);
Reading value keys from a cookie.
//Assuming user comes back after several hours. several < 12. //Read the cookie from Request. HttpCookie myCookie = Request.Cookies["myCookie"]; if (myCookie == null) { //No cookie found or cookie expired. //Handle the situation here, Redirect the user or simply return; } //ok - cookie is found. //Gracefully check if the cookie has the key-value as expected. if (!string.IsNullOrEmpty(myCookie.Values["UserId"])) { string UserId= myCookie.Values["UserId"].ToString(); //Yes UserId is found. Mission accomplished. } if (!string.IsNullOrEmpty(myCookie.Values["UrlSlug"])) { string UrlSlug = myCookie.Values["UrlSlug"].ToString(); //Yes key2 is found. Mission accomplished. }
If you need to bother at all, the authentication cookie, although not advisable, can do so.
Writing value keys to a cookie.
//create a cookie HttpCookie myCookie = FormsAuthentication.GetAuthCookie("UserName", true); //Add key-values in the cookie myCookie.Values.Add("UserId", "your-UserId"); myCookie.Values.Add("UrlSlug", "your-UrlSlug"); //set cookie expiry date-time, if required. Made it to last for next 12 hours. myCookie.Expires = DateTime.Now.AddHours(12); //Most important, write the cookie to client. Response.Cookies.Add(myCookie);
Reading value keys from a cookie.
//Assuming user comes back after several hours. several < 12. //Read the cookie from Request. HttpCookie myCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (myCookie == null) { //No cookie found or cookie expired. //Handle the situation here, Redirect the user or simply return; } //ok - cookie is found. //Gracefully check if the cookie has the key-value as expected. if (!string.IsNullOrEmpty(myCookie.Values["UserId"])) { string UserId= myCookie.Values["UserId"].ToString(); //Yes UserId is found. Mission accomplished. } if (!string.IsNullOrEmpty(myCookie.Values["UrlSlug"])) { string UrlSlug = myCookie.Values["UrlSlug"].ToString(); //Yes key2 is found. Mission accomplished. }