【问题标题】:HttpContext.Current.User.Identity.Name is lostHttpContext.Current.User.Identity.Name 丢失
【发布时间】:2012-09-09 06:00:54
【问题描述】:

我在我的 Web 应用程序中使用 ASP.NET 表单身份验证。最近我发现了一个奇怪的行为。在生产环境中一切正常。使用 .net 4.0 和 IIS 7。在登录用户中输入用户名和密码并登录然后突然HttpContext.Current.User.Identity.Name 丢失。这不会每次都发生,只是在某些情况下。我无法在我的开发环境中重现该问题。我检查了if(HttpContext.Current.User.Identity.IsAuthenticated) 这也是真实的身份验证票用户数据也不为空。只有HttpContext.Current.User.Identity.Name 是空的。请帮助

登录按钮中的代码

protected void LoginButton_Click(object sender, EventArgs e)
    {
        try
        {
            dtUserDetails = new DataTable();
            if (UserRepositoryBL.ValidateUser(txtUserName.Text.Trim(), Password.Text.Trim(), out dtUserDetails))
            {

                AuthUser au = new AuthUser();
                if (dtUserDetails.Rows.Count > 0)
                {
                    DataRow DR = dtUserDetails.Rows[0];
                    au.UserID = Convert.ToInt32(DR["UserID"].ToString());
                    au.UserNo = DR["UserNo"].ToString();
                    au.UserName = DR["UserName"].ToString();
                    au.Password = DR["Password"].ToString();
                }
                string userData = au.ToString();

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(

             2,                             // Version number

             txtUserName.Text.Trim(),      // Username

             DateTime.Now,                  // Issue date

             DateTime.Now.AddMinutes(60), // Expiration date

             false,                         // Persistent?

             userData                 // User data

         );



                string eticket = FormsAuthentication.Encrypt(ticket);

                if (Request.Cookies[txtUserName.Text] != null)
                {
                    //HttpCookie myCookie = new HttpCookie(txtUserName.Text);
                    //myCookie.Expires = DateTime.Now.AddDays(-1d);
                    Request.Cookies[txtUserName.Text].Expires = DateTime.Now.AddDays(-1d);
                    Request.Cookies.Remove(txtUserName.Text);
                }

                HttpCookie cookie = new HttpCookie("SiteCookie", eticket);
               // HttpCookie cookie = new HttpCookie("SiteCookie", eticket);
                cookie.Expires = DateTime.Now.AddMinutes(60);

                FormsAuthentication.SetAuthCookie(txtUserName.Text, false);
               // cookie.Path = FormsAuthentication.FormsCookiePath;
                FormsAuthentication.RenewTicketIfOld(ticket); 

                Response.Cookies.Add(cookie);



                BasePage.ActivityLog("User Login", txtUserName.Text.Trim(), true, Request.RawUrl);
                string url = FormsAuthentication.GetRedirectUrl(txtUserName.Text, false);

                Response.Redirect(url);

                //  FormsAuthentication.RedirectFromLoginPage(LoginUser.UserName, false);

            }
            else
            {
                FailureText.Text = "Your login attempt was not successful. Please try again.";
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

web.config

<authentication mode="Forms">
            <forms loginUrl="~/Account/Login.aspx" timeout="60" cookieless="UseCookies" defaultUrl="~/Landing.aspx" protection="All"/>
        </authentication>

        <authorization>
            <deny users="?" />

        </authorization>

【问题讨论】:

    标签: asp.net iis login forms-authentication


    【解决方案1】:

    您的会话时间限制为 60 分钟。问题只发生在会话过期的人身上吗?可能会解释为什么您不能在您的开发机器上重现它,因为您根本不需要等待那么久?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多