我也有同樣的問題。其實,它是因爲我無法從javascript讀取表單身份驗證cookie,這是過了一段時間undefined
。我只是想知道我是否通過JavaScript進行身份驗證。
後來我發現票已過期,但我沒有註銷(也是,所以我想解決這個問題)!我看到你的問題沒有得到答覆,所以我在半天內解決問題時保持開放。以下是我想到的,似乎正在工作。
我的答案是基於這個答案。 https://stackoverflow.com/a/454639/511438由用戶ScottS
這是在我的ASP.NET MVC 3項目。
這是我的登錄代碼。未顯示,它之前的自定義用戶身份驗證邏輯。這只是設置最初的票。
公共類FormsAuthenticationService:IFormsAuthentication
public void SignIn(string userName, bool createPersistentCookie, string role)
{
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1, // version
userName, // user name
DateTime.Now, // created
DateTime.Now.Add(FormsAuthentication.Timeout), // expires
false, // rememberMe?
role // can be used to store roles
);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
HttpContext.Current.Response.Cookies.Add(authCookie);
}
在同一類,但一個靜態方法是從Global.asax的
//-- this is based on https://stackoverflow.com/questions/454616/asp-net-cookies-authentication-and-session-timeouts
internal static FormsAuthenticationTicket RefreshLoginCookie(bool retainCurrentExpiry)
{
HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie == null || authCookie.Value == null)
return null;
FormsAuthenticationTicket oldTicket = FormsAuthentication.Decrypt(authCookie.Value);
DateTime expiryDate = (retainCurrentExpiry ? oldTicket.Expiration : DateTime.Now.Add(FormsAuthentication.Timeout));
HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
var newTicket = new FormsAuthenticationTicket(oldTicket.Version, oldTicket.Name, oldTicket.IssueDate, expiryDate,
oldTicket.IsPersistent, oldTicket.UserData, oldTicket.CookiePath);
HttpCookie newAuthCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(newTicket));
HttpContext.Current.Response.Cookies.Add(newAuthCookie);
return newTicket;
}
全球訪問。asax
我的自定義來自於ajax請求不刷新表單身份驗證票證。所以如果你在那裏等待超時,ajax請求會將你註銷。如果你想讓Ajax請求保持活着(改變我的javascript cookie問題,而不是你的註銷不活動的問題),改變這一點。 *(提示,如果你註銷,然後登錄,但又回到登錄頁面,第一次登錄沒有在查詢字符串中指定returnUrl)。 *
protected virtual void Application_AuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie == null || authCookie.Value == "")
{
return;
}
bool isAjax = new HttpRequestWrapper(System.Web.HttpContext.Current.Request).IsAjaxRequest();
FormsAuthenticationTicket authTicket;
try
{
//-- THIS IS WHAT YOU WANT
authTicket = FormsAuthenticationService.RefreshLoginCookie(isAjax);
}
catch
{
return;
}
string[] roles = authTicket.UserData.Split(';');
if (Context.User != null) Context.User = new GenericPrincipal(Context.User.Identity, roles);
}
的Web.config 這裏是我設置會話超時和票證超時部分
<configuration>
<system.web>
<sessionState mode="InProc" timeout="60" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="60" name="ProviderMvcSession" cookieless="UseCookies" />
</authentication>
但會話狀態與AuthenticationTicket/.ASPXAUTH cookie分開。至少我認爲這是?! – bplus
如果你不使用cookies來存儲這些數據,那麼會話負責處理它。 –
但我正在使用cookies!我有Cookieless =「UseCookies」。感謝您的迴應。我真的很困惑 - 你的建議可行,但我不知道爲什麼? – bplus