[英]ASP.NET 3.5 MVC1 - Users randomly unauthenticated/required to log back in
我們使用IIS 7使用進程外SQL會話狀態,ASP.NET 3.5 MVC 1.0和表單身份驗證。
用戶的會話在登錄時正確設置,並將按預期超時,將其重定向到特殊的“超時”登錄頁面。 問題是一些(並非所有)用戶登錄並且(從我所知)立即未經身份驗證並且需要重新登錄(即重定向到原始的“登錄”頁面)。
任何人都可能知道為什么我們的用戶會被間歇性地踢出去?
編輯:我已經在每個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>
回答上面對.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);
}
}
}
}
如果用戶未經過身份驗證,或者他們無權查看他們嘗試訪問的資源,ASP.Net會將用戶重定向到登錄頁面。
你有一個負載均衡的網站嗎?
如果是這樣,所有節點上的machineKeys是否相同? Forms Cookie名稱是否相同? 如果這些值存在差異,您可以在一個節點上登錄,並且似乎未經身份驗證到另一個節點。
這可能很愚蠢,但returnUrl是否可以指向登錄頁面?
當用戶已經登錄時,登錄頁面的行為如何?
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);
}
}
Spidey sense表示ASP.NET進程或應用程序池正在回收您。 可能由於一些原因而發生。 由於會話不在進行中,因此不會被此活動吃掉。
我會查看事件日志,看看是否有任何例子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.