猿问

FormsAuthentication.SignOut()不会将用户注销

FormsAuthentication.SignOut()不会将用户注销

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

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

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


慕后森
浏览 1024回答 3
3回答

慕盖茨4494581

用户仍然可以浏览您的网站,因为在您拨打电话时不会清除Cookie,FormsAuthentication.SignOut()并且每次新请求都会对其进行身份验证。在MS文档中说cookie将被清除,但它们没有,bug?与它完全相同Session.Abandon(),cookie仍然存在。您应该将代码更改为:FormsAuthentication.SignOut();Session.Abandon();// clear authentication cookieHttpCookie 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参考。

慕姐8265434

听起来像你没有正确设置你的web.config授权部分。请参阅下面的示例。<authentication&nbsp;mode="Forms"> &nbsp;&nbsp;<forms&nbsp;name="MyCookie"&nbsp;loginUrl="Login.aspx"&nbsp;protection="All"&nbsp;timeout="90"&nbsp;slidingExpiration="true"></forms></authentication><authorization> &nbsp;&nbsp;<deny&nbsp;users="?"&nbsp;/></authorization>
随时随地看视频慕课网APP
我要回答