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已成功执行并且return true. 问题是成功重定向后它无法知道身份验证状态。

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

添加

我阅读了FormsAuthentication.SetAuthCookie. 并cookieless="UseCookies"在 的Forms元素中添加Web.config。希望确保将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里面有一个名为 的 cookie FwLoginCookie,重定向到LoginSuccess.aspx这个 cookie 后就丢失了。请帮助审查它。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

4

1 回答 1

2

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

如果不对。请纠正我。谢谢。

于 2013-12-18T06:44:21.520 回答