LoginPage.aspx: -自定义验证模块继承IHttpModule的问题
protected void Button1_Click(object sender, EventArgs e)
{
Context.Items["Username"] = txtUserId.Text;
Context.Items["Password"] = txtPassword.Text;
//
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, Context.Items["Username"].ToString(), DateTime.Now, DateTime.Now.AddMinutes(10), true, "users", FormsAuthentication.FormsCookiePath);
// Encrypt the cookie using the machine key for secure transport
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName, // Name of auth cookie
hash); // Hashed ticket
// Set the cookie's expiration time to the tickets expiration time
if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
Response.Cookies.Add(cookie);
Response.Redirect("Default.aspx");
}
Global.asax文件: -
void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id =
(FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
// Get the stored user-data, in this case, our roles
string userData = ticket.UserData;
string[] roles = userData.Split(',');
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
Response.Write(HttpContext.Current.User.Identity.Name);
Response.Redirect("Default.aspx");
}
}
}
}
我收到以下错误
This webpage has a redirect loop.
The webpage at http://localhost:1067/Default.aspx has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.
为什么我得到重定向错误? – 2011-02-04 07:22:15
`Application_AuthenticateRequest`在每个*请求*上触发,因此当您登录时,您会按预期发送到Default.aspx。但是,在该页面呈现之前,将调用此代码并将浏览器重定向到Default.aspx。但是在渲染这段代码之前,它再一次将浏览器重定向到Default.aspx。重复这个过程,直到检测到(如果你幸运的话)无限重定向。 – 2011-02-04 20:49:36
另外,请不要更改您的整个帖子。您可以通过修改来修改它,但是您已经从IHttpModule切换到使用global.asax事件,这些事件是分开的。如果你走向不同的方向,请创建一个新问题,并参考旧的相关内容。 – 2011-02-04 20:52:14