Search code examples
c#asp.netasp.net-mvcasp.net-mvc-4asp.net-membership

My custom MembershipProvider "does not implement inherited abstract member 'System.Web.Security.MembershipProvider.*"


I'm trying to make a custom MembershipProvider, so I created a class called CustomMemberhsipProvider, and I made it inherit from MembershipProvider:

namespace Solution.Models
{
    public class CustomMembershipProvider : MembershipProvider
    {
        public bool CheckPassword(string password, string SaltedHash)
        {
            //code
        }

        public string CreateHash(string saltAndPassword)
        {
            //code
        }

        public string GetUserRoles(string userName)
        {
            //more code etc.
        }
    }
}

Then I referenced it in my web.config file like this:

<membership defaultProvider="CustomMembershipProvider">
  <providers>
    <clear />
    <add name="CustomMembershipProvider" type="Solution.Models.CustomMembershipProvider" enablePasswordRetrieval="false" maxInvalidPasswordAttempts="3" passwordAttemptWindow="10" applicationName="/"/>
  </providers>
</membership>

PROBLEM: I get a zillion run-time errors telling me that CustomMembershipProvider does not implement inherited abstract member MembershipProvider.ApplicationName.get / MembershipProvider.ApplicationName.set / MembershipProvider.ChangePassword(string, string, string) etc.

QUESTION: What exactly is going on here? Shouldn't CustomMembershipProvider inherit all of the properties and functions of MembershipProvider without any extra work? Am I missing something simple?

I did some research on this, and I am assuming by virtue of this answer that the issue is Visual Studio automatically filling in every property missing from CustomMembershipProvider with a throw new NotImplementedException();. Nevertheless, that particular answer didn't help me resolve the issue.


Solution

  • I'm pretty sure those are compile-time errors and not runtime errors that you are seeing.

    MembershipProvider is an abstract class, and in order to create a concrete implementation of an abstract class, you must provide implementations for (at least) all of its abstract members. Your class only has three explicit members (none of which are implementing MembershipProvider's abstract members), whereas by my count, the MembershipProvider has 27 abstract members, so you are at least 27 members short.

    MSDN has an article on how to implement custom membership providers, and it contains the text:

    The following tables list the required properties and methods that you must implement from the ProviderBase and MembershipProvider abstract classes and a description of each. To review an implementation of each member, see the code supplied for the Sample Membership Provider Implementation.

    followed by a very long table of properties and methods.

    In addition to the above article, I also suggest reading up on abstract classes as well, because you don't seem to be quite clear on what inheriting an abstract class entails.

    And here are some helpful articles that include information on setting up identity in ASP.NET using the built-in functionality (the TYPE CAST EXCEPTION blog has several different posts on ASP.NET identity).

    Deploy a Secure ASP.NET MVC 5 app with Membership, OAuth, and SQL Database to an Azure Website

    Customizing profile information in ASP.NET Identity in VS 2013 templates

    TYPE CAST EXCEPTION - ASP.NET MVC and Identity 2.0: Understanding the Basics