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

时间:2013-12-18 01:44:50

标签: c# asp.net form-authentication

我在测试中使用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已成功执行,return true。问题是成功重定向后无法知道已验证的状态。

我不知道我是否错过了什么或做错了什么。如果有 。请帮忙告诉我。谢谢。

我阅读了FormsAuthentication.SetAuthCookie的源代码。并在cookieless="UseCookies"的{​​{1}}元素中添加Forms。希望确保将cookie添加到Web.config(这由源代码Response完成)。仍然无效。

HttpContext.Current.Response.Cookies.Add(cookie)

http捕获详细信息如下所示。在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); } } 中有一个名为LoginTest.aspx的Cookie,在重定向到FwLoginCookie后,此Cookie丢失。请帮忙复习。

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:2)

终于明白为什么会发生这种奇怪的事情!这是因为还有一个名为ACA_USER_READ_ANNOUNCEMENT的cookie发送给响应。它是如此大的大小(超过5800字节),浏览器(在我的测试中它是IE)将忽略所有的cookie包括表单身份验证cookie(约300字节)。 但是当遇到这种情况时(巨大的cookie大小),其他浏览器如chrome / firefox与IE的行为不同。

如果不对。请你好好指正。感谢。

相关问题