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.
なぜリダイレクトエラーが発生しますか? –
'Application_AuthenticateRequest'はすべての*リクエスト*で起動します。ログインすると、期待通りにDefault.aspxに送られます。ただし、そのページがレンダリングされる前に、このコードが呼び出され、ブラウザがDefault.aspxにリダイレクトされます。しかし、もう一度このコードをレンダリングする前に、もう一度ブラウザをDefault.aspxにリダイレクトします。このプロセスは何かが無限のリダイレクトを検出するまで(あなたが運が良ければ)繰り返されます。 –
また、投稿全体を変更しないでください。変更を加えて修正することはできますが、IHttpModuleからglobal.asaxイベントを使用するように切り替えました。これらは別々です。別の方向に進む場合は、新しい質問を作成し、該当する場合は古いものを参照してください。 –