【问题标题】:asp.net web forms apps disallowing multi-user sessionasp.net web 表单应用程序不允许多用户会话
【发布时间】:2016-05-16 02:39:05
【问题描述】:

我的 asp.net 4.5 Web 表单应用程序不允许多个会话或会话超时等。第一个或两个人成功登录并使用系统,直到第三个或更多人尝试登录并将他们重定向到登录页面。按 F12 我收到以下消息

密码字段出现在带有不安全 (http://) 表单的表单中 行动。这是一个安全风险,允许用户登录凭据 被盗

这是我的登录按钮代码:

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        ApplicationDbContext _db = new ApplicationDbContext();
        var userStore = new UserStore<ApplicationUser>(_db);
        var userManager = new UserManager<ApplicationUser>(userStore);
        ApplicationUser user = userManager.Find(txtUserName.Text, txtPassword.Text);
        if (user != null)
        {
            if (user.IsDeleted && user.UserName.ToLower() != ApplicationDbInitializer.userName.ToLower())
            {
                ModelState.AddModelError("Error", "Your account has been deleted.");

            }
            else if (!user.IsActive && user.UserName.ToLower() != ApplicationDbInitializer.userName.ToLower())
            {
                ModelState.AddModelError("Error", "Your account has been disabled.");
            }
            else
            {
                IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                ClaimsIdentity identity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                AuthenticationProperties props = new AuthenticationProperties();
                props.IsPersistent = chkRememberMe.Checked;
                authenticationManager.SignIn(props, identity);
                if (!string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
                {
                    Response.Redirect(Request.QueryString["ReturnUrl"]);
                }
                else if (userManager.IsInRole(user.Id, "Admin"))
                {
                    Response.Redirect("~/admin/index");
                }
                else
                {
                    Response.Redirect("~/user/index");
                }
            }
        }
        else
        {
            ModelState.AddModelError("Error", "Invalid username or password.");
        }
    }

【问题讨论】:

    标签: c# asp.net session webforms


    【解决方案1】:
    <sessionState timeout="2880"></sessionState>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" defaultUrl="~/" />
    </authentication>
    <authorization>
      <deny users="?" />
      <allow users="*" />
    </authorization>
    <compilation targetFramework="4.5">
    

    【讨论】:

      【解决方案2】:

      好的。试试这个。希望它有效。

          <system.web>
          <sessionState cookieless="true" regenerateExpiredSessionId="true" timeout="2880" />
      <authentication mode="Forms">
        <forms loginUrl="~/Account/Login" timeout="2880" defaultUrl="~/" />
      </authentication>
      <authorization>
        <deny users="?" />
        <allow users="*" />
      </authorization>
          <compilation debug="true" targetFramework="4.5.2"/>
          <httpRuntime targetFramework="4.5.2"/>
          <httpModules>
            <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
          </httpModules>
        </system.web>
      

      【讨论】:

      • 嗨,我还包括: 仅包括 在我的 web.config 文件中
      • 是的。甚至包括httpModules
      • 正如您所说,该应用程序允许 2 个用户登录。但是如果有两个以上的用户登录,那么会话是否对所有用户都结束了?还是会话仅针对前 2 个用户以外的额外用户结束?
      【解决方案3】:

      您是否通过 HTTPS 部署站点?如果是这样,我认为您的登录 &lt;form&gt; 标签中有一个硬编码的 Yeo,它指向一个不安全的 HTTP url。更新标签以包含相对路径,并且不要在 HTML 中对协议进行编码。

      【讨论】:

      • 我没有通过 https 部署。你提到的代码中硬编码的Yeo标签在哪里?你能提供更多信息吗???
      • 在 aspx 页面中,我认为您正在寻找像 &lt;form runat="server"&gt; 这样的标签。通常在母版页上。不过,已经很久了,所以我可能记错了。
      猜你喜欢
      • 2011-11-28
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多