The problem you are facing is probably related to the cookie domain. A cookie may be written on "." + FormsAuthentication.CookieDomain "." + FormsAuthentication.CookieDomain . I previously set cookies to the admin.example.com domain and saw that the cookie was added to . . In a dev environment, it is written to localhost
The solution I'm using is to add two cookies for each cookie and session cookie.
So, I am using the following solution:
protected void SignOut(HttpContext Context) { FormsAuthentication.SignOut(); Context.Session.Abandon(); // clear authentication cookie Context.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName) { Path = FormsAuthentication.FormsCookiePath, Value = "", Domain = (Convert.ToString(FormsAuthentication.CookieDomain).Length > 0) ? Convert.ToString(FormsAuthentication.CookieDomain) : Context.Request.Url.Host, HttpOnly = true, Expires = DateTime.Now.AddYears(-1) }); Context.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName) { Path = FormsAuthentication.FormsCookiePath, Value = "", Domain = (Convert.ToString(FormsAuthentication.CookieDomain).Length > 0) ? "." + Convert.ToString(FormsAuthentication.CookieDomain) : "." + Context.Request.Url.Host, HttpOnly = true, Expires = DateTime.Now.AddYears(-1) }); // clear session cookie (not necessary for the current problem but recommended anyway) Context.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId") { Path = FormsAuthentication.FormsCookiePath, Value = "", Domain = (Convert.ToString(FormsAuthentication.CookieDomain).Length > 0) ? Convert.ToString(FormsAuthentication.CookieDomain) : Context.Request.Url.Host, HttpOnly = true, Expires = DateTime.Now.AddYears(-1) }); Context.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId") { Path = FormsAuthentication.FormsCookiePath, Value = "", Domain = (Convert.ToString(FormsAuthentication.CookieDomain).Length > 0) ? "." + Convert.ToString(FormsAuthentication.CookieDomain) : "." + Context.Request.Url.Host, HttpOnly = true, Expires = DateTime.Now.AddYears(-1) }); FormsAuthentication.RedirectToLoginPage(); }
The result of this call will add the following headers to the response
Location: /Login.aspx ReturnUrl = Default.aspx
Set-Cookie: **** =; expires = Tue, 12-Oct-1999 05:00:00 GMT; Path = /; HttpOnly
Set-Cookie: **** =; domain = admin.example.com; expires = Wed, 23-Apr-2014 18:04:58 GMT; Path = /; HttpOnly
Set-Cookie: **** =; domain = .admin.example.com; expires = Wed, 23-Apr-2014 18:04:58 GMT; Path = /; HttpOnly
Set-Cookie: ASP.NET_SessionId =; domain = admin.example.com expires = Wed, 23-Apr-2014 18:04:58 GMT; Path = /; HttpOnly
Set-Cookie: ASP.NET_SessionId =; domain = .admin.example.com expires = Wed, 23-Apr-2014 18:04:58 GMT; Path = /; HttpOnly
Where *** is the name of my cookie containing my encrypted authentication ticket value;
Note that the first Set-Cookie is most likely generated by a call to the FormsAuthentication.SignOut() method.
Brett Caswell
source share