2013-12-18 308 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的要求仅适用于已认证的用户,并且会话不够长,则使用用户的配置文件。 –

相关问题