【问题标题】:ASP.Net MVC app logout not completely logging outASP.Net MVC 应用程序注销未完全注销
【发布时间】:2013-08-19 07:29:25
【问题描述】:

此应用运行在某些用户仍在使用 IE7 的环境中,如果这有什么不同的话。我们偶尔会看到,在有人注销而其他人登录后,他们仍然会从前一个人那里得到残留物,这可能会显示该人的个人资料。任何建议将不胜感激。

我在我的 asp.net mvc 应用程序中使用以下作为注销方法

public ActionResult LogOff()
{

    System.Web.HttpContext.Current.Response.Cookies.Clear();
    FormsService.SignOut();
    Session["User"] = null;
    Session.Clear();
    Session.Abandon();
    Session.RemoveAll();

    return Redirect("/");
}

该应用正在使用保存在数据库中的会话,因为它在两个不同的 Web 服务器上运行。

这是来自 web.config 的一些设置

<sessionState sqlConnectionString="LiveDB" />
<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
<membership>
  <providers>
    <clear />
    <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="LiveDB" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="50" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
  </providers>
</membership>
<profile>
  <providers>
    <clear />
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="LiveDB" applicationName="/" />
  </providers>
</profile>
<roleManager enabled="true">
  <providers>
    <clear />
    <add connectionStringName="LiveDB" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
    <add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
  </providers>
</roleManager>

【问题讨论】:

  • 仅供参考,您似乎正在使用 FormsAuthentication 但您将用户信息存储在会话中。这可能不是一个好主意。会话和身份验证是两个不同的东西,具有不同的生命周期,更重要的是安全性。

标签: asp.net-mvc session authentication asp.net-mvc-4 forms-authentication


【解决方案1】:

如果你使用 FormAuthentication 这样登录 -

FormsAuthentication.SetAuthCookie("username", false);

那么注销应该是

FormsAuthentication.SignOut();

如果仍有问题,您可以强制 cookie 过期,如 this

【讨论】:

    【解决方案2】:

    MembershipSession 提供程序分开工作。两个成员可以使用一个会话。这不是规则,但可以。

    我不确定,但我对您的问题有一个建议。 Session 具有属性 IsNewSession。微软说,它“获取一个值,指示会话是否是使用当前请求创建的。”

    因此,您可以尝试检查登录用户的Session 是否是新用户,因为他可能与旧用户共享会话,并且可能是一个原因,为什么一个人会看到其他人的个人资料。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多