gpt4 book ai didi

asp.net - FormsAuthentication.SignOut() 不会注销用户

转载 作者:行者123 更新时间:2023-12-03 04:26:12 35 4
gpt4 key购买 nike

把我的头砸得太久了。如何防止用户在使用 FormsAuthentication.SignOut 注销后浏览网站页面?我希望这样做:

FormsAuthentication.SignOut();
Session.Abandon();
FormsAuthentication.RedirectToLoginPage();

但事实并非如此。如果我直接输入 URL,我仍然可以浏览到该页面。我已经有一段时间没有使用自己的安全性了,所以我忘记了为什么这不起作用。

最佳答案

用户仍然可以浏览您的网站,因为当您调用 FormsAuthentication.SignOut() 时,cookie 不会被清除,并且它们会在每个新请求时进行身份验证。 MS文档中说cookie将被清除,但他们没有,bug?和Session.Abandon()一模一样,cookie还在。

您应该将代码更改为:

FormsAuthentication.SignOut();
Session.Abandon();

// clear authentication cookie
HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
cookie1.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie1);

// clear session cookie (not necessary for your current problem but i would recommend you do it anyway)
SessionStateSection sessionStateSection = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState");
HttpCookie cookie2 = new HttpCookie(sessionStateSection.CookieName, "");
cookie2.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie2);

FormsAuthentication.RedirectToLoginPage();

HttpCookie 位于 System.Web 命名空间中。 MSDN Reference .

关于asp.net - FormsAuthentication.SignOut() 不会注销用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/412300/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com