2011-11-22 11 views
1
FormsAuthenticationUserData userData = new FormsAuthenticationUserData(member.Id, member.Role, member.Gender); 
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, member.UserName, DateTime.Now, DateTime.Now.AddHours(24), true, userData.Serialize()); 
string encTicket = FormsAuthentication.Encrypt(ticket); 
HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); 
faCookie.Expires = DateTime.Now.AddHours(25); 
Response.Cookies.Add(faCookie); 

string redirectUrl = FormsAuthentication.GetRedirectUrl(member.UserName, false); 
Response.Redirect(redirectUrl, true); 

protected void Application_AuthenticateRequest(object sender, EventArgs e) 
{ 
    HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; 
    if (authCookie != null) 
    {    
     FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); 
     try 
     { 
      WebIdentity identity = new WebIdentity(authTicket.Name, authTicket.UserData); 
      WebPrincipal currentMember = new WebPrincipal(identity); 
      Context.User = currentMember; 
     } 
     catch 
     { 
      FormsAuthentication.SignOut(); 
      FormsAuthentication.RedirectToLoginPage(); 
      Response.End(); 
     } 
    } 
} 

用戶無法登錄時,客戶端日期時間比服務器日期時間大不同(做餅乾空,無法登錄)Asp.net表單登錄不可能的,當客戶日期時間從服務器

有什麼解決辦法嗎?

thanx很多!

+0

您正在將cookie設置爲最近25小時。如果客戶端在服務器時間提前25小時提前設置,或者提前一分鐘,您是否說這不起作用? –

+0

例如,當客戶端的日期時間不正確並且10天大於服務器時。登錄的條件是客戶端日期時間必須與服務器日期 – mmtemporary

回答

1

看完您的評論後,這是預期的行爲,無法更改。該系統正在做它意味着什麼。您可以稍後設置cookie過期或使用滾動超時,但是,我認爲沒有理由讓機器的日期時間到目前爲止。

+0

相似或接近我的想法web應用程序必須獨立於客戶端系統日期時間。 – mmtemporary

+0

沒有cookie。瀏覽器正在收到一個過期的cookie,並忽略它應該如此。 –

+0

我們可以將客戶端日期時間發送到服務器。並添加25小時到cookie – mmtemporary

0

該技術避免完全使用瀏覽器的日期/時間。

  1. 將FormsAuthentication cookie設置爲永不過期,或在100年後過期。

  2. 將基於服務器時間的實際到期時間存儲在authenticationTicket.Expiration屬性中。見here

  3. 服務器驗證請求後,應該檢查authenticationTicket.Expiration是否已過期。我不是100%確定系統會自動執行此操作,您可能需要掛接到Application_AuthenticateRequest事件並自行完成。

  4. 如果已過期,則Web服務器應拒絕該請求;呈現HTTP 403狀態代碼和set-cookie標頭以在該點刪除cookie。

相關問題