MVC 5 Custom UserStore

18,420

This is actually not that hard.

Create a new class that implements the IUserStore interface.

public class MyUserStore : IUserStore<User> { }

...then implement its methods. I think there are 5 or 6 to start off with, that deal with creating / updating / deleting / finding users. If you are using entity framework for your user entities, then just constructor inject an instance of it into your user store instance.

public class MyUserStore : IUserStore<User>
{
    private readonly MyDbContext _dbContext;
    public MyUserStore(MyDbContext dbContext) { _dbContext = dbContext; }
}

Then, in the interface methods, just delegate to your dbcontext:

Task IUserStore<User>.CreateAsync(User user)
{
    _dbContext.Set<User>().Create(user);
    return Task.FromResult(0);
}

There are then a lot of other optional interfaces you can implement to support additional features. For example, if you want your UserStore to be able to handle password encryption for you, implement IUserPasswordStore:

public class MyUserStore : IUserStore<User>, IUserPasswordStore<User> { }

... and so on for the other interfaces like IUserRoleStore, etc.

What might really help is a decompilation tool like .NET Reflector or ReSharper's Navigate To Decompiled Sources feature. If you decompile the default UserStore in the Microsoft.AspNet.Identity.EntityFramework library, you can sort of see how they did it. Some of the decompiled code can be hard to read because some of the methods are async, but you should get the gist of it.

Share:
18,420

Related videos on Youtube

r3plica
Author by

r3plica

SOreadytohelp

Updated on June 06, 2022

Comments

  • r3plica
    r3plica almost 2 years

    I need to build a custom UserStore (basically because I have some requirements that are not part of the framework by default) but I can not find any documentation anywhere on how to do this.

    I have seen two pieces of information that are close:

    1. http://kazimanzurrashid.com/posts/implementing-user-confirmation-and-password-reset-with-one-asp-dot-net-identity-pain-or-pleasure - Which appears to apply to the earlier edition :(
    2. and MVC 5 IoC and Authentication - Which is similar to what I require but I need to see some code :(

    Basically I need to know how to create a user and how to sign in. The rest I can figure out. I guess the main issue is handling passwords, etc.

    Does anyone know of any documentation anywhere that might help me? Or if there is a breakdown on the default UserStore and it's internal methods that would be great (I could adapt from there).

    • Moritz Schmidt
      Moritz Schmidt almost 7 years
      First link seems to be dead..
  • r3plica
    r3plica over 10 years
    thank you, that is a great idea. I will have a look at .Net Reflector. I could really do with looking at some of the features shipped with .Net. Cheers
  • danludwig
    danludwig over 10 years
    Note if you use Reflector, it may not be able to decompile every method. I tried it myself to see if I could get a better reading than ReSharper, but it ended up that Reflector had some issues decompiling async methods.
  • r3plica
    r3plica over 10 years
    I downloaded ILSpy and it seems to decompile fine, but I will post here if there is something it doesn't work with :D
  • yardpenalty.com
    yardpenalty.com about 9 years
    There are so many unique interfaces in the UserStore that it may be a good idea to create UserManagers for specific tasks by injecting a specific interface. For instance, I am going to create a UserManager injecting only the IQueryableUserStore to replace my repository that typically uses EF methods for db persistence.