无法使用FormsAuthentication.SignOut()从ASP.NET MVC应用程序注销

我试图在ASP.NET MVC中实现Logoutfunction。

我为我的项目使用表单身份validation。

这是我的退出代码:

FormsAuthentication.SignOut(); Response.Cookies.Clear(); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, FormsAuthentication.FormsCookieName, DateTime.Today.AddYears(-1), DateTime.Today.AddYears(-2), true, string.Empty); Response.Cookies[FormsAuthentication.FormsCookieName].Value = FormsAuthentication.Encrypt(ticket); Response.Cookies[FormsAuthentication.FormsCookieName].Expires = DateTime.Today.AddYears(-2); return Redirect("LogOn"); 

此代码将用户重定向到登录屏幕。 但是,如果我通过在地址栏中指定名称来调用操作方法(或从地址栏下拉列表中选择上一个链接),我仍然可以在不登录的情况下访问安全页面。

有人可以帮我解决这个问题吗?

这很奇怪……我只打了一次电话:FormsAuthentication.SignOut(); 它的工作原理……

 public ActionResult Logout() { FormsAuthentication.SignOut(); return Redirect("~/"); } 

要正确回答您的问题,我必须知道如何保护您的“安全”页面。
我怀疑你在那里做错了什么。

FormsAuthentication.SignOut()简单调用应该足够了,因为它会清除身份validationcookie,从而使您在其他方法调用时变得多余。

使用ASP.NET MVC,您必须在操作方法上使用AuthorizeAttribute来禁止未经身份validation的访问者使用它。 (含义:通过在Web.config指定位置标记,使用Web窗体执行此操作的旧方法不再适用于MVC 。)

例如,这是我的ForumController类的一个小代码片段:

 public class ForumController : Controller { ... [Authorize] public ActionResult CreateReply(int topicId) { ... } ... } 

以下问题与我的解决方案有关

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

如果不在web.config文件中禁用[comment]以下标记来轻松测试Web应用程序,则此方法有效。

 public ActionResult SignOut() { FormsAuthentication.SignOut(); return RedirectToAction("Index", "Home"); } 

web.config中