C# asp.net未将用户注销
我的头又撞到砖墙上了。我正试图让我的ASP.NET Web窗体Web应用程序注销,但它拒绝这样做。我正在使用表单身份验证。问题似乎是浏览器(我尝试过的所有浏览器)在登录后维护页面缓存,但在注销时没有清除该缓存 当我点击主页上的注销链接时,它会成功地将我转移到登录页面,但我只需键入页面的URL或在浏览器上按back键,它就可以再次加载页面而无需登录 我花了几个小时在StackOverflow和其他地方寻找解决方案,但到目前为止没有任何效果 My root web.config具有以下功能:C# asp.net未将用户注销,c#,asp.net,forms-authentication,C#,Asp.net,Forms Authentication,我的头又撞到砖墙上了。我正试图让我的ASP.NET Web窗体Web应用程序注销,但它拒绝这样做。我正在使用表单身份验证。问题似乎是浏览器(我尝试过的所有浏览器)在登录后维护页面缓存,但在注销时没有清除该缓存 当我点击主页上的注销链接时,它会成功地将我转移到登录页面,但我只需键入页面的URL或在浏览器上按back键,它就可以再次加载页面而无需登录 我花了几个小时在StackOverflow和其他地方寻找解决方案,但到目前为止没有任何效果 My root web.config具有以下功能: <
<authentication mode ="Forms">
<forms loginUrl="~/Account/Login" name=".ASPXFORMSAUTH" defaultUrl="~/Default.aspx"></forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
在我的Page_Init(同样是母版页cs文件)中,我有以下内容:
protected void Page_Init(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
HttpContext.Current.Response.AddHeader("Expires", "0");
// The code below helps to protect against XSRF attacks
var requestCookie = Request.Cookies[AntiXsrfTokenKey];
Guid requestCookieGuidValue;
if (requestCookie != null && Guid.TryParse(requestCookie.Value, out requestCookieGuidValue))
{
// Use the Anti-XSRF token from the cookie
_antiXsrfTokenValue = requestCookie.Value;
Page.ViewStateUserKey = _antiXsrfTokenValue;
}
else
{
// Generate a new Anti-XSRF token and save to the cookie
_antiXsrfTokenValue = Guid.NewGuid().ToString("N");
Page.ViewStateUserKey = _antiXsrfTokenValue;
var responseCookie = new HttpCookie(AntiXsrfTokenKey)
{
HttpOnly = true,
Value = _antiXsrfTokenValue
};
if (FormsAuthentication.RequireSSL && Request.IsSecureConnection)
{
responseCookie.Secure = true;
}
Response.Cookies.Set(responseCookie);
}
Page.PreLoad += master_Page_PreLoad;
}
最后,在我的母版页标题中,我有这些元标记
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="cache-control" content="no-store" />
<meta http-equiv="cache-control" content="must-revalidate" />
<meta http-equiv="cache-control" content="proxy-revalidate" />
通常情况下,我在请求帮助之后,在进行更多搜索并点击灯泡片刻后,才发现问题所在 如果其他人有问题,以下是对我有效的解决方案 虽然它设置为使用表单身份验证,但当我回顾默认的asp.net登录页面和关联的cs页面时,我意识到它使用的是Identity.Owin命名空间进行登录 因此,在注销时,我将上面的第一段代码替换为:
public void Logout_Click(object sender, EventArgs e)
{
ClearSession();
FormsAuthentication.RedirectToLoginPage();
}
protected void ClearSession()
{
Context.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
Session.Abandon();
}
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="cache-control" content="no-store" />
<meta http-equiv="cache-control" content="must-revalidate" />
<meta http-equiv="cache-control" content="proxy-revalidate" />
public void Logout_Click(object sender, EventArgs e)
{
ClearSession();
FormsAuthentication.RedirectToLoginPage();
}
protected void ClearSession()
{
Context.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
Session.Abandon();
}