无法在第二个会员提供商中获取个人资料和角色

时间:2016-02-05 01:20:08

标签: c# mysql asp.net

我在我的应用程序中使用两个成员资格提供程序,当用户键入用户名和密码时,它应检查第一个成员资格提供程序中的用户名,如果不在第一个提供程序中,则只检查第二个成员资格提供程但问题是,每次搜索第二个成员资格提供者时,配置文件和角色提供者仍然是第一个提供者。我在代码中做错了什么? 以下是我的代码:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    MembershipProvider ProviderName = Membership.Providers["MySqlMembershipProvider2"];


    if (Membership.ValidateUser(Login1.UserName, Login1.Password) == true)
    {
        e.Authenticated = true;
        Login1.DestinationPageUrl = "~/Start.aspx";
    }
    else
    {
        if (Membership.Providers["MySqlMembershipProvider2"].ValidateUser(Login1.UserName, Login1.Password) == true)
        {

            RoleProvider RoleName= Roles.Providers["MySqlRoleProvider2"];
            ProviderName.GetUser(Login1.UserName,true);
            e.Authenticated = true;
            Login1.DestinationPageUrl = "~/CL/CLLogin.aspx";
        }
        else
        {
            e.Authenticated = false;
        }
    }
}

的web.config:

<roleManager defaultProvider="MySQLRoleProvider" enabled="true">
    <providers>
    <clear/>
    <add name="MySQLRoleProvider" connectionStringName="MySQLMembershipConnection" applicationName="/" type="MySql.Web.Security.MySQLRoleProvider" autogenerateschema="true"/>
    <add name="MySQLRoleProvider2" connectionStringName="MySQLMembershipConnection2" applicationName="/" type="MySql.Web.Security.MySQLRoleProvider" autogenerateschema="true"/>
  </providers>
</roleManager>
<anonymousIdentification enabled="true"/>
<membership defaultProvider="MySQLMembershipProvider">
  <providers>
    <clear/>
    <add name="MySQLMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider" connectionStringName="MySQLMembershipConnection" autogenerateschema="true" applicationName="/" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"/>
    <add name="MySQLMembershipProvider2" type="MySql.Web.Security.MySQLMembershipProvider" connectionStringName="MySQLMembershipConnection2" autogenerateschema="true" applicationName="/" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"/>
  </providers>
</membership>
<profile defaultProvider="MySQLProfileProvider">
  <providers>
    <clear/>
    <add name="MySQLProfileProvider" type="MySql.Web.Profile.MySqlProfileProvider" applicationName="/" connectionStringName="MySQLMembershipConnection" autogenerateschema="true"/>
    <add name="MySQLProfileProvider2" type="MySql.Web.Profile.MySqlProfileProvider" applicationName="/" connectionStringName="MySQLMembershipConnection2" autogenerateschema="true"/>
  </providers>
  <properties>
    <add name="FullName" provider="MySQLProfileProvider"/>
    <add name="FName" provider="MySQLProfileProvider"/>
    <add name="ClassName" provider="MySQLProfileProvider2"/>
    <add name="Area" provider="MySQLProfileProvider2"/>
  </properties>
</profile>
<sessionState>
  <providers>
    <add name="MySqlSessionStateStore" type="MySql.Web.SessionState.MySqlSessionStateStore,MySql.Web"/>
    <add name="MySqlSessionStateStore2" type="MySql.Web.SessionState.MySqlSessionStateStore,MySql.Web"/>
  </providers>
</sessionState>

1 个答案:

答案 0 :(得分:0)

您的login_authenticate未正确实现,您需要处理使用这两个提供程序进行身份验证。首先检查默认提供程序(如果是大小写),然后针对第二个提供程序(在if else中)进行身份验证。这是一个代码示例。您不需要在authenticate方法的开头硬编码provider2。

代码示例:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        bool foundUser = false;
        List<string> roles = new List<string>();
        roles.Add("GeneralUser");
        // this will call the default MembershipProvider
        if (Membership.Provider.ValidateUser(LoginBox.UserName, LoginBox.Password))
        {
            foundUser = true;
            // do any additional lookups for this type of user (Default MembershipProvider) here
        } // otherwise, explicitly call secondary provider
        else if (
            Membership.Providers["SecondarySqlMembershipProvider"].ValidateUser(LoginBox.UserName,
                                                                                LoginBox.Password))
        {
            foundUser = true;
            roles.Add("SecondaryUser");
            // do any additional lookups relevant to this type of user
        }
        if (foundUser)
        {
            Session["UserId"] = LoginBox.UserName;
            Session["Groups"] = roles;
        }
        e.Authenticated = foundUser;
    }

希望这有帮助。