2011-01-31 27 views
3

我們聘請所需外的過程SQL會話狀態,ASP.NET MVC 3.5和1.0表單中使用IIS 7ASP.NET 3.5 MVC1 - 用戶隨機未經認證/重新登錄

用戶的會話驗證是在登錄時正確設置,並按預期超時,將其重定向到特殊的「超時」登錄頁面。問題在於一些(不是全部)用戶登錄,並且(從我所知道的)立即未經身份驗證並且需要重新登錄(即重定向到原始「登錄」頁面)。

任何人都可能有一個想法,爲什麼我們的用戶間歇性被踢出?

編輯:我已經在每個Application_AuthenticateRequest事件上添加了日誌記錄,我可以告訴你,在用戶啓動之前,Auth票證都已通過身份驗證,持續兩天後過期並且請求也通過身份驗證。到達登錄頁面後,用戶不再通過身份驗證。

編輯#2:我們取得了一些進展,看起來好像用戶可能未經過身份驗證,因爲此Web應用程序正在尋找父應用程序中的腳本和其他內容,而其中的用戶未通過身份驗證。列入這些腳本的原始格式如下:

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script> 

我已將其更正爲:

<script src="<%= Url.Content("~/Scripts/MicrosoftAjax.js") %>" type="text/javascript"></script> 

ANSWER在我們的.master頁以上改變我們的腳本引用解決了問題。它明確告訴應用程序查看當前應用程序的根文件夾。感謝所有幫助過的人。我希望我可以標記不止一個答案!

下面是我們的登錄操作:

[AcceptVerbs(HttpVerbs.Post)] 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", 
     Justification = "Needs to take same parameter type as Controller.Redirect()")] 
    public virtual ActionResult LogOn(string userName, string password, string returnUrl) 
    { 
     if ((HttpContext.Current.User == null) || (!HttpContext.User.Identity.IsAuthenticated)) 
     { 
      if (!ValidateLogOn(userName, password)) 
      { 
       try 
       { 
       return View(); 
       } 
       catch (Exception ex) 
       { 
       throw new Exception(string.Format("User validation failed at LogOn: {0}", ex.ToString())); 
       } 
      } 
     } 

     bool rememberMe = true; 

     FormsAuth.SignIn(userName, rememberMe); 

     Session["userName"] = userName; 

     if (!String.IsNullOrEmpty(returnUrl)) 
     { 
      try 
      { 
       return Redirect(returnUrl); 
      } 
      catch (Exception ex) 
      { 
       throw new Exception(string.Format("User redirect to returnUrl ({0}) failed: {1}", returnUrl, ex.ToString())); 
      } 
     } 
     else 
     { 
      try 
      { 
       return RedirectToAction("Index", "RodWebUI"); 
      } 
      catch (Exception ex) 
      { 
       throw new Exception(string.Format("User redirect to action: Index, controller: RodWebUi failed: {0}", ex.ToString())); 
      } 
     } 
    } 

下面是我們timeoutlogon行動:

public virtual ActionResult TimeOutLogon() 
    { 
     try 
     { 
      FormsAuth.SignOut(); 

      ViewData["TimeoutMsg"] = "Session timed out. Please log back in."; 

      return View(); 
     } 
     catch (Exception ex) 
     { 
      throw new Exception(string.Format("Error with redirecting to TimeOutLogon: {0}", ex.ToString())); 
     } 
    } 

因爲我已經添加了以下檢查,以我們的Global.asax中記錄的當前狀態請求和授權票。在被踢回LogOn之前,所有事情都經過認證和確定。

protected void Application_AuthenticateRequest(object sender, EventArgs e) 
    { 
     if (HttpContext.Current.User != null) 
     { 
      if (HttpContext.Current.User.Identity.IsAuthenticated) 
      { 
       if (HttpContext.Current.User.Identity is FormsIdentity) 
       { 
        FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity; 

        FormsAuthenticationTicket ticket = identity.Ticket; 

        LogFunctionCall(HttpContext.Current.User.Identity.Name, "", "User Authentication Check", "", 
             string.Format("Auth ticket is expired: {0}, expiration date: {1}, is persistent: {2}, issued: {3}", 
             ticket.Expired, ticket.Expiration, ticket.IsPersistent, ticket.IssueDate), "", 0); 

        LogFunctionCall(HttpContext.Current.User.Identity.Name, "", "User Authentication Check Line #2", "", 
             string.Format("Raw URL: {0}, Request is authenticated: {1}", HttpContext.Current.Request.RawUrl, HttpContext.Current.Request.IsAuthenticated), "", 0); 
       } 
      } 
     } 
    } 

回答

2

你有一個負載均衡的網站嗎?

如果是這樣,所有節點上的機器鍵是否相同? Forms Cookie名稱是否相同?如果這些值存在差異,您可以在一個節點上登錄並且似乎對另一個節點未經驗證。

1

這可能是愚蠢的,但可以在RETURNURL被指向到登錄頁面?

當用戶已經登錄時,登錄頁面的行爲如何?

+0

不,returnUrl沒有指向登錄頁面。用戶登錄並導航到我們網站上的另一個頁面時,他們被重定向到登錄頁面,並且在重定向之前,returnUrl顯示了預期的URL。如果用戶已經登錄並且他們要導航到登錄頁面(他們將通過在地址欄中輸入url來完成登錄),他們將被註銷,因爲不提供密碼。 – alan 2011-01-31 21:33:41

+0

@alan:偏題:在這種情況下,我不會註銷用戶。簡單的重定向到「索引」會更安全。 – msms 2011-01-31 21:39:01

+0

我注意到,我還會更新我的登錄操作代碼以反映新的更改。而不是重新驗證用戶,我會檢查用戶是否通過身份驗證。 – alan 2011-01-31 22:17:02

2

如果用戶沒有通過身份驗證,或者他們沒有權限查看他們正嘗試訪問的資源,則ASP.Net會將用戶重定向到登錄頁面。

+0

這些頁面上的所有資源都可以在用戶被要求重新登錄之前訪問(例如,用戶應該在一個點和幾秒鐘後訪問一個頁面,重新訪問該頁面才能啓動) – alan 2011-02-02 14:59:28

+0

您是否擁有可靠的方法重現這個問題?你能確定地知道,當你提出某個要求時,問題會顯示出來嗎?如果是這樣,我會在某處設置一個斷點(global.asax中的BeginRequest將是一個好地方),然後檢查請求,看看auth cookie是否存在。有可能是刪除auth cookie(可以在客戶端完成)。 – 2011-02-02 15:08:15

0

Spidey sense表示ASP.NET進程或應用程序池正在對您進行回收。可能發生的原因有幾個。由於會議過程中,他們不會被這個事件吃掉。

我會查看事件日誌,看看是否有任何hinkey正在發生。

1
public partial class MasterPage : System.Web.UI.MasterPage 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    } 
    protected void signout_Click(object sender, EventArgs e) 
    { 
     Response.Write("<script language=javascript>var wnd=window.open('','newWin','height=1,width=1,left=900,top=700,status=no,toolbar=no,menubar=no,scrollbars=no,maximize=false,resizable=1');</script>"); 
     Response.Write("<script language=javascript>wnd.close();</script>"); 
     Response.Write("<script language=javascript>window.open('login.aspx','_parent',replace=true);</script>"); 
     Session["name"] = null; 
    } 
} 

我也加入所有網頁的代碼上。

protected void Page_Load(object sender, EventArgs e) 
    { 
     Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     Response.Cache.SetAllowResponseInBrowserHistory(false); 
    } 
} 
相關問題