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

Posted by David Lively on Stack Overflow See other posts from Stack Overflow or by David Lively
Published on 2011-01-03T18:33:49Z Indexed on 2011/01/03 18:54 UTC
Read the original article Hit count: 431

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 .

© Stack Overflow or respective owner

Related posts about c#

Related posts about membership-provider