2012-09-09 27 views
0

我在我的web應用程序中使用ASP.NET窗體身份驗證。最近我發現了一個奇怪的行爲。一切工作正常在生產environment.with NET 4.0和IIS 7.在登錄用戶輸入用戶名和密碼登錄然後突然HttpContext.Current.User.Identity.Name lost.this不會發生每次只有在某些場合。我無法重現在我的開發environment.i問題已經檢查if(HttpContext.Current.User.Identity.IsAuthenticated)這也是事實驗證票證的用戶數據不爲空also.only HttpContext.Current.User.Identity.Name是empty.plz幫助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> 

回答

0

您的會話時間限制爲60分鐘。問題只發生在會話過期的人羣中嗎?也許可以解釋爲什麼你不能在你的開發機器上重現這一點,因爲你根本就不會等那麼久?