私は ASP.NET Web サイトに取り組んでおり、カスタムのシンプルなログイン メカニズムを使用する必要があります。有名なEmployee Info Starter Kitから始めました
これが私がこれまでに持っているものです:
ASP.NET ページで:
protected void ButtonLogOn_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(txtUserName.Value) || String.IsNullOrEmpty(txtPassword.Value))
labelMessage.Text = MessageFormatter.GetFormattedErrorMessage("You can login using a username and a password associated with your account. Make sure that it is typed correctly.");
else
{
//if the log-in is successful
LoginPage LoginBack = new LoginPage();
if (LoginBack.VerifyCredentials(txtUserName.Value, txtPassword.Value) == 0)
{
SiteLogin.PerformAuthentication(txtUserName.Value, checkBoxRemember.Checked);
}
else
{
labelMessage.Text = MessageFormatter.GetFormattedErrorMessage("<strong>Login Failed!</strong><hr/>The username and/or password you entered do not belong to any User account on our system.<br/>You can login using a username and a password associated with your account. Make sure that it is typed correctly.");
}
}
}
protected void ButtonAdminLogOn_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(txtUserName.Value) || String.IsNullOrEmpty(txtPassword.Value))
labelMessage.Text = MessageFormatter.GetFormattedErrorMessage("<strong>Login Please!</strong><hr/>You can login using a username and a password associated with your account. Make sure that it is typed correctly.");
else
{
//if the log-in is successful
if (txtUserName.Value == "admin" && txtPassword.Value == "123123")
{
SiteLogin.PerformAdminAuthentication("admin", checkBoxRemember.Checked);
}
else
{
labelMessage.Text = MessageFormatter.GetFormattedErrorMessage("<strong>Login Failed!</strong><hr/>The username and/or password you entered do not belong to any Administrator ccount on our system.<br/>You can login using a username and a password associated with your account. Make sure that it is typed correctly.");
}
}
}
そしてユーティリティクラス
public static void PerformAuthentication(string userName, bool remember)
{
FormsAuthentication.RedirectFromLoginPage(userName, remember);
if (HttpContext.Current.Request.QueryString["ReturnUrl"] == null)
{
RedirectToDefaultPage();
}
else
{
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.QueryString["ReturnUrl"]);
}
}
public static void PerformAdminAuthentication(string userName, bool remember)
{
FormsAuthentication.RedirectFromLoginPage(userName, remember);
if (HttpContext.Current.Request.QueryString["ReturnUrl"] == null)
{
RedirectToAdminDefaultPage();
}
else
{
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.QueryString["ReturnUrl"]);
}
}
私のログイン フォームには 2 つのボタンがあります。通常のログイン ルーチンは、Web サービスを呼び出す別のアセンブリに戻り、ドメイン ログインに対してチェックされたユーザー名とパスワードを取得します。
さて、コードを含むもう 1 つのファイルがあり、私を困惑させています。
Global.asax
<script RunAt="server">
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity.AuthenticationType != "Forms")
{
throw new InvalidOperationException("Only forms authentication is supported, not " +
HttpContext.Current.User.Identity.AuthenticationType);
}
IIdentity userId = HttpContext.Current.User.Identity;
//if role info is already NOT loaded into cache, put the role info in cache
if (HttpContext.Current.Cache[userId.Name] == null)
{
string[] roles;
if (userId.Name == "admin")
{
roles = new string[1] { "administrators" };
}
else if (userId.Name == "member1")
{
roles = new string[1] { "employees" };
}
else
{
roles = new string[1] { "public" };
}
//1 hour sliding expiring time. Adding the roles in cache.
//This will be used in Application_AuthenticateRequest event located in Global.ascx.cs
//file to attach user Principal object.
HttpContext.Current.Cache.Add(userId.Name, roles, null, DateTime.MaxValue, TimeSpan.FromHours(1), CacheItemPriority.BelowNormal, null);
}
//now assign the user role in the current security context
HttpContext.Current.User = new GenericPrincipal(userId, (string[])HttpContext.Current.Cache[userId.Name]);
}
}
}
</script>
この Web サイトには、無料でアクセスできる About ページがいくつかありますが、残りは管理者または従業員用です。管理者のユーザー名/パスワードは固定されていますが、従業員のログインはドメイン形式で入力され、ターゲット ドメインで検証する必要があり (すべて完了)、従業員の役割を設定する必要があります。
Global.asax ファイルの Application_AuthenticateRequest メソッドでそれを行うにはどうすればよいですか?