0

我在我的 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;
        }
    }

网络配置

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

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

        </authorization>
4

1 回答 1

0

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

于 2012-09-09T16:43:32.530 回答