在StackOverflow上回答this之后,Microsoft建议执行以下操作:
我在global.asax
文件中想出了这行代码,(我在用户表isLogin
中添加了一个额外的字段,在登录和注销页面中将其设置为true和false。 ..)
void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
IPrincipal p = HttpContext.Current.User;
if (p.Identity.IsAuthenticated)
{
var userName = p.Identity.Name;
HttpCookie authenticationCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (SecurityUtilities.CheckUserLoggedIn(userName) == false)
{
FormsAuthentication.SignOut();
Response.StatusCode = 401;
Response.End();
}
}
}
我想知道我做的这一切正确吗?这段代码呢?
if (SecurityUtilities.CheckUserLoggedIn(userName) == false)
{
FormsAuthentication.SignOut();
Response.Redirect(FormsAuthentication.LoginUrl);
}