2015-02-11 63 views
0

這裏是我的登錄密碼,問題發生在用戶登錄後重定向到「檔案」身份2.0用戶都被重定向回登錄

protected void UserLogin_Click(object sender, EventArgs e) 
     { 
      if (IsValid) 
      { 

       var manager = new IdentityModels.UserManager(); 

       IdentityModels.User user = manager.Find(Username.Text, Password.Text); 

       if (user != null) 
       { 
        if (isUserActive(user.PatientId)=="isNotActive") 
        { 
         lblError.Text = 
          "you are no longer active. Please contact your local clinic to find out why."; 
         return; 

        } 
        if (isUserActive(user.PatientId) == "clinicNotActive") 
        { 
         lblError.Text = 
          "Your clinic is no longer active. Please contact your local clinic to find out why."; 
         return; 

        } 
        IdentityModels.IdentityHelper.SignIn(manager, user, RememberMe.Checked); 

        if (manager.IsInRole(user.Id,"Administrator") || manager.IsInRole(user.Id,"Staff") || manager.IsInRole(user.Id,"Physician")) 
        { 
         Response.Redirect("Dashboard"); 
        } 


        if (Request.QueryString["Profile"] != null) 
        { 
         IdentityModels.IdentityHelper.RedirectToReturnUrl(Request.QueryString["Profile"], Response); 
        } 
        else 
        { 
         Response.Redirect("Profile"); 
        } 


       } 
       else 
       { 
        ModelState.AddModelError("", "Invalid username or password"); 
        lblError.Text = "Invalid username or password"; 
       } 
      } 
     } 

這裏是個人資料頁面上網頁的加載代碼:

var manager = new IdentityModels.UserManager(); 
      IdentityModels.User user = manager.FindById(HttpContext.Current.User.Identity.GetUserId()); 

     if (user == null) 
     { 

      var ex = new Exception("patient was null, BUT TRIED SIGNING IN NOW" + UserAccess.GetUserId().ToString()); 
       Elmah.ErrorSignal.FromCurrentContext().Raise(ex); 

      Response.Redirect("Login"); 
     } 

Elmah日誌顯示異常「患者爲空,但試圖簽名爲0」。

因此,如果我的用戶成功登錄,他們必須是因爲他們正在打開個人資料頁面,那麼爲什麼他們中的一些人會遇到此錯誤。爲什麼用戶爲空?

我只是無法弄清楚,爲什麼它會影響一些但不是全部。當我重新發佈網站時,所有用戶都可以登錄幾分鐘,有時幾個小時,然後重新開始。

+0

您使用表單身份驗證或Windows身份驗證? – Claies 2015-02-11 21:12:06

+0

@Claies我不確定,因爲它們都不在我的web.config中。 – prospector 2015-02-11 21:19:29

+0

那麼你可能使用表單身份驗證。 'HttpContext.CurrentUser.Identity'派生自成功登錄後在客戶端設置的cookie,其中包含身份驗證票據。如果cookie不可用或已過期,則「Identity」將爲空。因此,我會檢查用戶是否禁用了Cookie。 – Claies 2015-02-11 21:24:54

回答

1

好吧,這裏是答案。

將會話狀態從InProc(在我的情況下)更改爲SQLServer,自登錄重定向以來已經過了22個小時,但之前沒有發生過這種情況,所以我認爲可以安全地說問題已解決,這就是答案。

1

嘗試使用User.Identity而不是HttpContext.Current.User.Identity。我已經看到了一些情況,其中上下文(基於ASP.NET的會話)與Identity的令牌不同步。