gpt4 book ai didi

c# - ASP.NET 网站的自定义登录机制

转载 作者:行者123 更新时间:2023-11-30 17:59:44 28 4
gpt4 key购买 nike

我在 ASP.NET 网站上工作,我需要摆脱一些自定义但简单的登录机制。我从著名的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"]);
}
}

我的登录表单有两个按钮:管理员登录是硬编码的名称/密码。正常的登录例程返回到调用 Web 服务并根据域登录检查用户名和密码的另一个程序集。

现在,还有另一个文件包含代码,这让我感到困惑。

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>

该网站有一些允许免费访问的“关于”页面,但其余页面供管理员或员工使用。我的管理员用户名/密码是固定的,但员工登录名是以域格式输入的,需要在目标域上验证(全部完成),然后设置员工角色。

我如何在 Global.asax 文件的 Application_AuthenticateRequest 方法中执行此操作?

最佳答案

为不同的文件夹设置不同的授权模式(通过 Web.config 甚至只是 IIS snap-in ):

  • 根匿名(关于页面)
  • 为 ~/Admin 区域创建授权
  • 用于 ~/Employers 区域的 Windows/NTLM

您也可以使用扩展 Login control与自定义 Membership provider .

关于c# - ASP.NET 网站的自定义登录机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10846307/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com