代码之家  ›  专栏  ›  技术社区  ›  vijaysylvester

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

  •  4
  • vijaysylvester  · 技术社区  · 14 年前

    我正在尝试在ASP.NET MVC中实现注销功能。

    我对我的项目使用窗体身份验证。

    这是我的注销代码:

    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");
    

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

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

    4 回复  |  直到 7 年前
        1
  •  6
  •   Palantir    14 年前

    public ActionResult Logout() {
      FormsAuthentication.SignOut();
      return Redirect("~/");
    }
    
        2
  •  1
  •   Venemo    14 年前


    FormsAuthentication.SignOut()

    AuthorizeAttribute Web.config

    ForumController

    public class ForumController : Controller
    {
        ...
    
        [Authorize]
        public ActionResult CreateReply(int topicId)
        {
            ...
        }
    
        ...
    }
    
        4
  •  0
  •   Jahed Kabiri    7 年前

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

    <authentication mode="Forms">
      <forms name="Your Project Name" defaultUrl="/" loginUrl="/Users/Login" timeout="43200" />
    </authentication>
    
    <location path="Administrator">
      <system.web>
        <authorization>
          <allow roles="Administrator" />
          <deny users="*" />
        </authorization>
      </system.web>
    </location>
    
    <location path="UserPanel">
      <system.web>
        <authorization>
          <deny users="?" />
        </authorization>
      </system.web>
    </location>