Login.aspx Looping 和 GetRolesForUser 没有任何值
本文关键字:任何值 GetRolesForUser aspx Looping Login | 更新日期: 2023-09-27 18:35:06
我的 Web 窗体应用程序出现问题。 具体来说,每当函数 GetRolesForUser 返回 null 时,我的应用程序都会返回登录页面。这是我的应用程序的工作流程。用户首先使用登录页面登录,然后重定向默认值.aspx如果他是验证用户。但是,某些经过验证的用户(Member.ValidateUser)在重定向到默认值时.aspx不会在 GetRolesForUser( 为 null) 中返回角色。因此,即使我将角色="官员"设置为默认值,这些用户也无法重定向到不同的页面(员工/默认.aspx)。它仍然将它们重定向回登录.aspx。
我不知道是什么导致了这个问题。我检查应用程序名称,它是正确的。
有谁知道发生了什么以及导致此问题的原因?你能建议一种解决这个问题的方法吗?
网络.config
<location path="Default.aspx">
<system.web>
<authorization>
<allow roles="Master,CanEdit"/>
<allow roles="Admin,CanEdit"/>
<allow roles="Staff,CanEdit"/>
<allow roles="Officer"/>
<allow roles="Agent"/>
<allow roles="Front Desk"/>
<allow roles="Manager"/>
<deny users="?"/>
<!--<allow users="*"/>-->
</authorization>
</system.web>
</location>
<authentication mode="Forms">
<forms name="Login" loginUrl="~/Login.aspx" path="/" defaultUrl="~/Default.aspx" protection="All" timeout="60"/>
</authentication>
<membership defaultProvider="ApplMembershipProvider">
<providers>
<add name="ApplMembershipProvider"
connectionStringName="ApplConnection"
applicationName="/" enablePasswordRetrieval="false" enablePasswordReset="true"
requiresQuestionAndAnswer="false" requiresUniqueEmail="true"
passwordFormat="Hashed"
minRequiredPasswordLength="3" minRequiredNonalphanumericCharacters="0"
maxInvalidPasswordAttempts="30" type="System.Web.Security.SqlMembershipProvider"/>
</providers>
</membership>
登录.aspx
protected void Page_Load(object sender, EventArgs e)
{
TextBox username = (TextBox)loginControl.FindControl("UserName");
TextBox password = (TextBox)loginControl.FindControl("Password");
if (IsPostBack)
{
if (!String.IsNullOrEmpty(username.Text) && !String.IsNullOrEmpty(password.Text))
{
// set focus on the username text box when the page loads
username.Focus();
EmployeeSchool EmplSchool;
string test = ApplConfiguration.DbConnectionString;
EmplSchool = ApplSchoolUsers.GetEmployeeSchool(username.Text);
string connection = ApplSchoolUsers.GetConnectionString(EmplSchool.School);
ApplConfigurationSchool.ConfigureConnectionString(connection);
string test1 = ApplConfiguration.DbConnectionString;
if (Membership.ValidateUser(username.Text, password.Text))
{
string returnUrl = (string)Request.QueryString["ReturnUrl"];
if (returnUrl != null)
{
Response.Redirect("~/Default.aspx", false);
}
}
}
}
}
默认.aspx
string userName = "";
string[] UserRoles = null;
System.Web.Security.RoleProvider roleProvider = System.Web.Security.Roles.Provider;
try
{
string test = ApplConfiguration.DbConnectionString;
userName = Membership.GetUser().UserName.ToString();
//it does return a valid userName
UserRoles = roleProvider.GetRolesForUser(userName);
//testing
string currUserRole = (UserRoles.Length!=0) ? UserRoles[0] : "Officer";
switch (currUserRole)
{
case "Master":
Response.Redirect("~/Adm/DefaultMaster.aspx",false);
break;
case "Admin":
Response.Redirect("~/Adm/Default.aspx",false);
break;
case "Front Desk":
Response.Redirect("~/Lsi/Default.aspx",false);
break;
case "Staff":
case "Officer":
Response.Redirect("~/Staff/Default.aspx",false);
break;
case "Manager":
Response.Redirect("~/Manager/Default.aspx",false);
break;
case "Agent":
Response.Redirect("~/Agent/Default.aspx",false);
break;
}
}
你错过了太多的碎片 -
-
如果要使用角色提供程序,则需要在 web.config 中
<roleManager..></roleManager>
标记。 -
Member.ValidateUser 用于验证用户;您仍然需要使用
FormsAuthentication.SetAuthCookie(username , true|false);
创建 FormAuthentication Cookie -
您无需在 Default.aspx 中实例化
... roleProvider = System.Web.Security.Roles.Provider;
。你只需要打电话——string[] roles = Roles.GetRolesForUser(User.Identity.Name);