我在测试中使用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.config
的Forms
元素中添加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将丢失。请帮忙查看它。
对于单个用户?听起来像我的饼干被禁用... –
是的。仅供单个用户使用。问题是当我通过IE的IE开发人员工具检查详细信息时,在重定向页面(LoginSuccess.aspx)中不存在用于成功验证的cookie。谢谢。 –
而让我困惑的是其他用户的名字没有这个问题! :P –