当前位置: 首页 > 编程语言 > C#

ASP.NET网站共享的自定义登录机制

时间:2023-04-10 14:38:29 C#

ASP.NET网站的自定义登录机制我在ASP.NET网站上工作,我需要通过一些自定义但简单的登录机制。我从着名的员工信息进入套件开始这是我到目标之前停止所拥有的:在ASP.NET页面上:protectedvoidButtonLogOn_Click(objectsender,EventArgse){if(String.IsNullOrEmpty(txtUserName.Value)||String.IsNullOrEmpty(txtPassword.Value))labelMessage.Text=MessageFormatter.GetFormattedErrorMessage("您可以使用与您的帐户关联的用户名和密码登录。确保输入正确。");else{//如果登录成功LoginPageLoginBack=newLoginPage();如果(LoginBack.VerifyCredentials(txtUserName.Value,txtPassword.Value)==0){SiteLogin.PerformAuthentication(txtUserName.Value,checkBoxRemember.Checked);}else{labelMessage.Text=MessageFormatter.GetFormattedErrorMessage("登录失败!您输入的用户名和/或密码不属于我们系统上的任何用户帐户。您可以使用与您的帐户关联的用户名和密码登录。确保输入正确。");}}}protectedvoidButtonAdminLogOn_Click(objectsender,EventArgse){if(String.IsNullOrEmpty(txtUserName.Value)||String.IsNullOrEmpty(txtPassword.Value))labelMessage.Text=MessageFormatter.GetFormattedErrorMessage("请登录!您可以使用与您的帐户关联的用户名和密码登录.确保输入正确。");else{//如果登录成功if(txtUserName.Value=="admin"&&txtPassword.Value=="123123"){SiteLogin.PerformAdminAuthentication("admin",checkBoxRemember.Checked);}else{labelMessage.Text=MessageFormatter.GetFormattedErrorMessage("登录失败!您输入的用户名和/或密码不属于我们系统上的任何管理员帐户。您可以使用与您的帐户关联的用户名和密码登录。确保输入正确。");}}}和实用类publicstaticvoidPerformAuthentication(stringuserName,boolremember){FormsAuthentication.RedirectFromLoginPage(userName,remember);如果(HttpContext.Current.Request.QueryString["ReturnUrl"]==null){RedirectToDefaultPage();}else{HttpContext.Current.Response.Redirect(HttpContext.Current.Request.QueryString["ReturnUrl"]);}}publicstaticvoidPerformAdminAuthentication(stringuserName,boolremember){FormsAuthentication.RedirectFromLoginPage(userName,remember);如果(HttpContext.Current.Request.QueryString["ReturnUrl"]==null){RedirectToAdminDefaultPage();}else{HttpContext.Current.Response.Redirect(HttpContext.Current.Request.QueryString["ReturnUrl"]);我的登录表单有两个按钮:管理员登录是硬编码的名称/密码正常登录例程返回到另一个调用Web服务的程序集,并根据域登录检查用户名和密码。现在,还有另一个文件的代码让我感到困惑。Global.asax中protectedvoidApplication_AuthenticateRequest(Objectsender,EventArgse){if(HttpContext.Current.User!=null){if(HttpContext.Current.User.Identity.IsAuthenticated){if(HttpContext.Current.User.Identity.AuthenticationType!="Forms"){thrownewInvalidOperationException("只支持表单身份验证,不支持"+HttpContext.Current.User.Identity.AuthenticationType);}IIdentityuserId=HttpContext.Current.User.Identity;//如果角色信息尚未加载到缓存中,则将角色信息放入缓存中if(HttpContext.Current.Cache[userId.Name]==null){string[]roles;if(userId.Name=="admin"){roles=newstring[1]{"administrators"};}elseif(userId.Name=="member1"){roles=newstring[1]{"employees"};}else{roles=newstring[1]{“public”};}//1小时滑动过期时间。在缓存中添加角色。//这将在位于Global.ascx.cs的Application_AuthenticateRequest事件中使用//fileto附加用户主体对象。HttpContext.Current.Cache.Add(userId.Name,roles,null,DateTime.MaxValue,TimeSpan.FromHours(1),CacheItemPriority.BelowNormal,null);}//现在在当前安全上下文中分配用户角色HttpContext.Current.User=newGenericPrincipal(userId,(string[])HttpContext.Current.Cache[userId.Name]);该网站有一些允许免费访问的关于页面,但其余的是管理员或员工我的管理员用户名/密码是固定的但员工登录以域格式输入并且需要在目标域上进行身份验证(全部完成)和然后设置员工角色。我如何在Global.asax文件的Application_AuthenticateRequest方法中执行此操作?为不同的文件夹设置不同的身份验证模式(通过Web.config或什至只是IIS管理单元):您还可以将扩展登录控件与自定义成员资格提供程序一起使用定义登录机制共享的所有内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: