【问题标题】:Unable to logout from ASP.NET MVC application using FormsAuthentication.SignOut()无法使用 FormsAuthentication.SignOut() 从 ASP.NET MVC 应用程序注销
【发布时间】:2010-10-08 09:42:48
【问题描述】:

我正在尝试在 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");

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

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

【问题讨论】:

  • 你为什么不使用 FormsAuthentication.SignOut() ?
  • @Aliostad : 现在更改了源代码,之前方法调用被包装了。
  • @vijaysylvester - 您能否详细介绍一下您是如何确保安全网页的安全的?
  • 我已在 Web.Config 中将它们配置为拒绝未经身份验证的用户访问。
  • @vijaysylver - 你的问题。这不是 Web 表单,这是 MVC。 Venemo 的答案是正确的——你需要用授权来装饰动作方法。我敢打赌,即使您没有登录,您也可以访问任何安全页面。

标签: c# .net asp.net asp.net-mvc forms-authentication


【解决方案1】:

这很奇怪...我只调用一次:FormsAuthentication.SignOut();并且有效...

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

【讨论】:

  • 我使用了反射器并看到了 FormsAuth.SignOut() 在做什么。它和我想要完成的事情一样。就是将 cookie 的 expires 属性设置为前一个日期。但它小心地将 cookie 添加到响应中,但我不这样做,这就是这里的问题。感谢您的帮助!
【解决方案2】:

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

FormsAuthentication.SignOut() 的简单调用就足够了,因为它会清除身份验证cookie,从而使您在那里进行的其他方法调用变得多余。

使用 ASP.NET MVC,您必须在操作方法上使用 AuthorizeAttribute 以禁止未经身份验证的访问者使用它。 (意思是:通过在 Web.config 中指定位置标签来使用 Web 表单的旧方法不再适用于 MVC。)

例如,这是我的ForumController类中的一个小代码sn-p:

public class ForumController : Controller
{
    ...

    [Authorize]
    public ActionResult CreateReply(int topicId)
    {
        ...
    }

    ...
}

【讨论】:

  • 明确一点,即使您在服务器端使用 SignOut() 清除 cookie,客户端也会在下一次请求时传递 Forms Auth Cookie。为了防止这种情况,我添加了一个同名但过期的 cookie。
  • @vijaysylvester - 那么,你如何解释它对我和@Palantir 也有效?
  • @vijaysylvester - 你是正确的,cookie 仍然会被发送(直到它过期),但这并不重要,因为这个 cookie 将被视为 ASP.NET 的“陈旧”。还有其他步骤来支持这一点,例如绝对过期和 SSL——但添加同名 cookie 的“黑客”并不是其中之一。我不认为 FormsAuth 是这里的问题。
  • @Venemo:干杯伙伴!你们都在第一行成功了 FormsAuthentication.SignOut() ,因为我没有,我也尝试了其他选项。就这么简单。
  • 我很困惑,你是说你 didnt 在你的原始代码中有 FormsAuthentication.SignOut() 吗?那为什么会出现在问题中呢?
【解决方案3】:

以下问题与它的解决方案对我有用

FormsAuthentication.SignOut() does not log the user out

【讨论】:

    【解决方案4】:

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

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

    web.config

    <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>
    

    【讨论】:

      猜你喜欢
      • 2013-08-19
      • 2019-10-01
      • 2014-08-01
      • 1970-01-01
      • 2019-09-20
      • 2010-11-11
      • 2017-02-22
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多