Mukesh Sharma
Mukesh Sharma

Reputation: 9022

Custom membership provider in ASP.NET

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

Upvotes: 0

Views: 1192

Answers (3)

SOfanatic
SOfanatic

Reputation: 5573

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.

Upvotes: 0

Ronald
Ronald

Reputation: 2100

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.

Upvotes: 1

Joppe
Joppe

Reputation: 101

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

Upvotes: 0

Related Questions