It's likely the issue you were experience was in regards to the cookie domain. The cookie maybe written to "." + FormsAuthentication.CookieDomain
. I have set cookies to "admin.example.com" domain before, and have seen the cookie prepended with .
. In the dev environment, it is written to localhost
The solution I use is to add two cookies for each authentication cookie and session cookie.
So the solution I'm using is as followed:
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 into 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 likely generated from the FormsAuthentication.SignOut()
method call.