我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 后就丢失了。请帮助审查它。