0
votes

I am implementing my own membership provider.I used Repository pattern.

public interface IMembershipService: IDisposable
{
    void CreateUser(string username, string password, string email, string role);
    bool CheckUsernameExist(string username);
    string EncodePassword(string originalPassword,string salt);
    // Some other my admin methods
}
public class MembershipService : IMembershipService
{
    public MembershipService()
    {
    }
    public void ActivateUserAccount(string username, string email)
    {
        // Some validation code
    }
    public void CreateUser(string username, string password, string email, string role)
    {
        // Some validation code
    }
   }

I did not inherited MembershipProvider. Now, I want to know how to implement GetUser() which return the current logged in user.I have no idea about that. Second thing, by inheriting MembershipProvider, Can I used GetUser() or some other useful methods? I could not inherit that class as well , if I want to do so. Please guide me and don't downvote as answer to this question matter me most. Thanks

3
What is your reason for creating a custom membership provider? This functionality is not as easy as it seems, and 99% of people who do it themselves do it wrong (that is, do it insecurely).Erik Funkenbusch

3 Answers

1
votes

If you want to created your custom ASP.NET Membership provider, you MUST inherit from the abstract MembershipProvider class. If not, it will not fit into the entire ASP.NET Providers model.

To implement a membership provider, you create a class that inherits the MembershipProvider abstract class from the System.Web.Security namespace. The MembershipProvider abstract class inherits the ProviderBase abstract class from the System.Configuration.Provider namespace, so you must implement the required members of the ProviderBase class as well.

http://msdn.microsoft.com/en-us/library/f1kyba5e(v=vs.100).aspx

You will need to override all the methods and provide your own data access. Here's example code for an ODBC Provider Implementation.

0
votes

I recommend this library: Code First MembershipProvider. Just replace the database queries with calls to your repository.

0
votes

You are heading in the right track, what you have to do now is have your MembershipService implement both IMembershipService and MembershipProvider then you can do the following:

public class MembershipService : MembershipProvider, IMembershipService
{
    private readonly IUserRepository _userRep; // or whatever you are calling your user repository

    public MembershipService()
       : this(DependencyResolver.Current.GetService<IUserRepository>())
    {
    }

    public MembershipService(IUserRepository userRep)
    {
       this._userRep = userRep;
    }

    public override MembershipUser GetUser(string username, bool userIsOnline)
    {
    ...

Your IMembershipService interface should implement all the classes from MembershipProvider that you plan on using. You can then inject your IMembershipService into your controller.