2013-12-18 306 views
3

我在測試中使用Form Authentication。並且還有一些測試用戶名。但是爲特定名稱找到了一個奇怪的問題。這是所有的測試名稱,只有一個名爲amybeyond可以在測試中起作用。爲什麼FormsAuthentication.SetAuthCookie不起作用在IE

請幫我在我的測試中查看我的代碼。

LoginTest.aspx(這是爲用戶名和密碼輸入一個登錄表單。)

public partial class LoginTest : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void Button1_Click(object sender, EventArgs e) 
     { 
      //after succeed validating user. then redirect to LoginSuccess.aspx page. 
      bool bValidate=Membership.ValidateUser("amybeyond", "11111111"); 
      if (bValidate) 
      { 
       FormsAuthentication.SetAuthCookie("AmyBeyond", false); 
       Response.Redirect("LoginSuccess.aspx"); 
      } 

     } 
    } 

LoginSuccess.aspx(在此頁,只需簡單地測試是否當前的請求被重定向之後認證。)

public partial class LoginSuccess : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      //the HttpContext.Current.Request.IsAuthenticated always false in the IE. 
      if (HttpContext.Current.Request.IsAuthenticated) 
      { 
       Response.Write("ok, you login successfully."); 
      } 
     } 
    } 

我相信Membership.ValidateUser成功執行和return true。問題是它不能知道成功重定向後的身份驗證狀態。

我不知道我是否錯過了什麼或做錯了什麼。如果有 。請幫忙告訴我。謝謝。

新增

我讀的FormsAuthentication.SetAuthCookie的源代碼。並在Web.configForms元素中添加cookieless="UseCookies"。希望確保將cookie添加到Response(這由源代碼HttpContext.Current.Response.Cookies.Add(cookie)完成)。仍然不起作用。

public static void SetAuthCookie(string userName, bool createPersistentCookie, string strCookiePath) 
{ 
    Initialize(); 
    HttpContext current = HttpContext.Current; 
    if (!current.Request.IsSecureConnection && RequireSSL) 
    { 
     throw new HttpException(SR.GetString("Connection_not_secure_creating_secure_cookie")); 
    } 
    bool flag = CookielessHelperClass.UseCookieless(current, false, CookieMode); 
    HttpCookie cookie = GetAuthCookie(userName, createPersistentCookie, flag ? "/" : strCookiePath, !flag); 
    if (!flag) 
    { 
     HttpContext.Current.Response.Cookies.Add(cookie); 
     current.CookielessHelper.SetCookieValue('F', null); 
    } 
    else 
    { 
     current.CookielessHelper.SetCookieValue('F', cookie.Value); 
    } 
} 

新增

在HTTP捕獲下面詳細示出。在LoginTest.aspx有一個名爲FwLoginCookie的cookie,重定向到LoginSuccess.aspx後,此cookie將丟失。請幫忙查看它。

enter image description here

enter image description here

enter image description here

+0

對於單個用戶?聽起來像我的餅乾被禁用... –

+0

是的。僅供單個用戶使用。問題是當我通過IE的IE開發人員工具檢查詳細信息時,在重定向頁面(LoginSuccess.aspx)中不存在用於成功驗證的cookie。謝謝。 –

+0

而讓我困惑的是其他用戶的名字沒有這個問題! :P –

回答

2

終於拿到爲什麼會出現這種奇怪的事情發生了!這是因爲有一個名爲ACA_USER_READ_ANNOUNCEMENT的cookie被髮送到響應。它的尺寸非常大(超過5800字節),因此瀏覽器(在我的測試中它是IE)會忽略包含表單身份驗證cookie(大約300字節)的所有cookie。 但是遇到這種情況時(巨大的cookie大小),其他瀏覽器如chrome/firefox與IE不同。

如果不對。請好心糾正我。謝謝。

+0

查看關於cookie大小的信息,看起來不能超過4096個字節https://discussions.apple.com/message/18820657#18820657 –

+0

我在考慮是否有其他機制將數據存儲在客戶端(瀏覽器)存儲。特別是對於大數據。謝謝。 –

+0

將其存儲在會話中,或者如果存儲> 4KB的要求僅適用於已認證的用戶,並且會話不夠長,則使用用戶的配置文件。 –

相關問題