为什么FormsAuthentication.SetAuthCookie在IE中不起作用

3
我在我的测试中使用了表单身份验证 (Form Authentication)。同时还有一些测试用户名。但是发现一个特别奇怪的问题,就是除了一个名叫amybeyond的测试用户名之外,所有的测试用户名都可以在测试中工作。
请帮忙查看我的测试代码。

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已经成功执行并返回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


对于单个用户?听起来像是禁用了Cookie... - Simon Whitehead
让我困惑的是其他用户的名字没有这个问题! :P - Joe.wang
请让该用户检查他们在IE中的安全设置。很明显它没有保存cookies。 - Simon Whitehead
很抱歉我没有表达清楚。我的意思是所有的用户名都在同一台电脑上进行测试。 - Joe.wang
1
您的用户名区分大小写吗?看起来您正在验证小写,但在设置Cookie时使用了正确的大小写。 - Brian Rogers
显示剩余2条评论
1个回答

2
最终弄明白了为什么会出现这种奇怪的情况!原因是还有一个名为ACA_USER_READ_ANNOUNCEMENT的cookie被发送到响应中。它非常大(超过5800字节),导致浏览器(在我的测试中是IE)会忽略所有的cookie,包括表单身份验证cookie(约300字节)。但其他浏览器如Chrome / Firefox在遇到这种情况时(巨大的cookie大小)行为不同于IE。如果我有任何错误,请指正。谢谢。

关于Cookie大小的问题,请参考此链接,看起来您不能指望超过4096字节。https://discussions.apple.com/message/18820657#18820657 - ta.speot.is
我在思考是否有其他机制可以将数据存储在客户端(浏览器)存储中,特别是对于大量数据。谢谢。 - Joe.wang
将它存储在会话中,如果需要存储的要求仅适用于经过身份验证的用户且会话不够长,则使用用户的个人资料。 - ta.speot.is
会话并不存储在浏览器中,而是存储在服务器端而非客户端。谢谢。 - Joe.wang
你可能已经解决了这个问题,但如果你想在客户端存储更多的数据,你应该看一下localstorage。根据这篇文章,最大的大小似乎是10MB:https://dev59.com/questions/s3A75IYBdhLWcg3w_umD。 - Johan Gov
嗯...但我不确定它是否仅适用于HTML5浏览器? - Joe.wang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接