HttpContext.Current.User.Identity.Name丢失

时间:2012-09-09 06:00:55

标签: asp.net iis login forms-authentication

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

登录按钮中的代码

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>

1 个答案:

答案 0 :(得分:0)

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