简体   繁体   中英

ASP.NET MVC3 Custom Membership Provider - The membership provider name specified is invalid

I'm implementing a custom membership provider, and everything seems to go swimmingly until I create a MembershipUser object. At that point, I receive the error:

The membership provider name specified is invalid. Parameter name: providerName

In web.config the membership key is

<membership defaultProvider="MembersProvider">
  <providers>
    <clear/>
    <add name="MembersProvider" type="Members.Providers.MembersProvider" connectionStringName="ApplicationServices"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
         applicationName="DeviceDatabase" />
  </providers>
</membership>

When creating the MembershipUser object from my custom User class:

    public static MembershipUser ToMembershipUser(User user)
    {
        MembershipUser member = new MembershipUser
            ("MembersProvider"
            , user.Name
            , user.Id
            , user.EmailAddress
            , user.PasswordQuestion
            , user.Comment
            , user.IsApproved
            , user.IsLockedOut
            , user.DateCreated
            , user.LastLoginDate ?? DateTime.MinValue
            , user.LastActivityDate ?? DateTime.MinValue
            , user.LastPasswordChangedDate ?? DateTime.MinValue
            , user.LastLockoutDate ?? DateTime.MinValue
            );

        return member;
    }

(I realize I could probably just inherit my User class from MembershipUser, but it's already part of an existing class hierarchy. I honestly think this is the first time I've encountered a legitimate need for for multiple inheritance!)

My feeling is that the new MembershipUser(...) providerName parameter is supposed to match what's set in web.config , but, since they match already, I'm at a loss as to how to proceed.

Is there a convenient way to get the name of the active membership provider in code?

I'm starting to think that using the built-in membership system is overkill and more trouble than it's worth.

Edit Not sure if it's relevant, but the custom membership provider class is in a class library, not the main WAP project.

Update Here's the contents of the System.Web.Security.Membership.Provider object as show in the VS2010 command window:

>eval System.Web.Security.Membership.Provider
{Members.Providers.MembersProvider}
    [Members.Providers.MembersProvider]: {Members.Providers.MembersProvider}
    base {System.Configuration.Provider.ProviderBase}: {Members.Providers.MembersProvider}
    ApplicationName: null
    EnablePasswordReset: true
    EnablePasswordRetrieval: false
    MaxInvalidPasswordAttempts: 5
    MinRequiredNonAlphanumericCharacters: 0
    MinRequiredPasswordLength: 6
    PasswordAttemptWindow: 10
    PasswordFormat: Function evaluation was aborted.
    PasswordStrengthRegularExpression: Cannot evaluate expression because debugging information has been optimized away .
    RequiresQuestionAndAnswer: Cannot evaluate expression because debugging information has been optimized away .
    RequiresUniqueEmail: Cannot evaluate expression because debugging information has been optimized away .

Update 2

This just randomly started working, which means I changed something but can't remember what it was. Stupid brain. I'll accept the only answer that's been posted and update this if I figure out what the problem was.

I used Membership.Provider.Name to get the correct name parameter

    public static MembershipUser GetUserFromEntity(this UserEntity userEntity)
    {
        return new MembershipUser(
                Membership.Provider.Name, 
                userEntity.Username,
                userEntity.PartitionKey,
                userEntity.Email,
                userEntity.PasswordQuestion,
                userEntity.Comment,
                userEntity.IsApproved,
                userEntity.IsLockedOut,
                userEntity.CreationDate,
                userEntity.LastLoginDate,
                userEntity.LastActivityDate,
                userEntity.LastPasswordChangedDate,
                userEntity.LastLockedOutDate
            );
    }

Not the solution yet, but with the two following functions you can at least get an idea what is registered:

Membership.Provider

Membership.Providers

I did inherit my User class from MembershipUser for some reason, but I'm pretty sure I had a good reason for it :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM