40

I'm very new to ASP.NET Identity and please bear with me if this question seems silly.

When I read the definition for the UserStore and the UserManager classes on the Microsoft website which are in the links below, it looks like both classes define operations around users (like Add, Find, Delete and Modify).

So when do I use one over the other?

https://msdn.microsoft.com/en-us/library/dn315446(v=vs.108).aspx https://msdn.microsoft.com/en-us/library/dn613290(v=vs.108).aspx

2
  • 2
    In term of Domain Driven Design Usermanager is Application Layer , it coordinate other layer Action , and userStorer is Repository layer , it interact with Database , now suppose you want to send sms to user , you cant use userStore , because its responsibility is just to do data persistence related task. but you can use userManager to send sms or other action.
    – Arash
    Commented Apr 18, 2016 at 10:19
  • 1
    No need to preface your question with an apology. If your question has already been asked it will certainly be marked as such. Some of the "silliest" questions are the most up-voted: Stack Overflow: Helping One Million Developers Exit Vim Commented Sep 25, 2018 at 13:49

3 Answers 3

60

Things are quite complicated there and could have been easier.

UserManger is the ... manager. It does not really interact with the storage, the database. That's what the UserStore does.

In fact UserManager has a constructor which needs a UserStore.

Why would you have to different object to manage users? Well, the main reason is you can decide not to use EF and create your own user store.

Things get clearer when you try to implement your own storage provider. I did it and my code can be downloaded from github.

This is the UserManager. As you can see there's not much in there. Just a few lines of code to configure the validator.

UserStore on the contrary, is quite big. In that example I've implemented a few interfaces and overridden a few methods. That's what you would do if you want to customize the interaction with the database and/or extend your classes.

You don't normally interact with the UserStore and in fact it's hidden. You just create it and pass it to the UserManager and ... forget about it.

You can always customize your UserManager and expose the UserStore:

public class UserManager : UserManager<User, int>
{
    public UserManager(IUserStore<User, int> store): base(store)
    {
        this.Store = store;
    }

    public IUserStore<User, int> Store { get; set; }

}

and, maybe, ovveride some of the methods:

public class UserManager : UserManager<User, int>
{
    public UserManager(IUserStore<User, int> store): base(store)
    {
        this.Store = store;
    }

    public IUserStore<User, int> Store { get; set; }

    public override System.Threading.Tasks.Task<IdentityResult> CreateAsync(User user)
    {
        return base.CreateAsync(user);
    }
}

but that would be pointless unless you have to do some peculiar customization.

Let's say you want to create a user using the store instead of the manager. You can do something like this:

await this.UserManager.Store.CreateAsync(new Custom.Identity.User() { UserName = "LeftyX" });

and it would work.

In the class above, as you can see, I've overridden the CreateAsync in the UserManager.
That method calls UserStore.CreateAsync() and in fact, you have to call the base method CreateAsync:

public override System.Threading.Tasks.Task<IdentityResult> CreateAsync(User user)
   {
       return base.CreateAsync(user);
   }

If you don't do that and, for example, return null instead, the UserStore.CreateAsync wouldn't be called and the user wouldn't be created.

It makes sense at the end.

I guess the best way to understand how this framework works is to try and customize/implement your solution with your own storage and see how all the classes interact with each other.

The sample project does not interact with a database but uses a json storage. It's very easy to debug. Give it a go and things will be clearer at some point.

13
  • Thanks a lot, I have clearer understanding on my question. But in your classes, you put all methods to UserStore class. but according to MS website, they have methods for operations against User in UserStore and UserManager. Those are different methods but why dont they just put all in UserStore or UserManager like yours? What is their purpose for doing that?
    – MinhNguyen
    Commented Jul 9, 2015 at 17:21
  • Upvote for a clearer more concise answer than my lanky long answer.
    – Adam
    Commented Jul 9, 2015 at 17:39
  • @Adam: Thanks. Appreciated.
    – LeftyX
    Commented Jul 9, 2015 at 17:55
  • 2
    @MinhNguyen: As I said UserStore is there cause some people want to implement their providers instead of EF. It's not exposed. You cannot customize UserManager to change the provider of data. You simple can't. You need to implement your UserStore. UserStore is the repository as in DDD.
    – LeftyX
    Commented Jul 9, 2015 at 19:39
  • 3
    Store = Repository / Manager = Service
    – Rod
    Commented Jan 12, 2016 at 12:43
9

I was watching Identity tutorial on YouTube, and I think this screenshot might help:

Managers vs Stores

So UserManager is the actual class that you should work with but it does not know how to store and retrieve data from database. it even does not know where the data is going to and coming from.

For those things it uses UserStore and says to it for example "Hey UserStore I have a new User that needs to be saved for future use, I don't know where you're going to save it and how you're going to do it, just save it for me"

Then the UserStore does the actual work such as where should the data be saved? which database? and how? the default one uses EF and SQL Server so if you want to use some other database for example MySQL you're going to need a different UserStore.

This is one of the features that were added to Identity compared to Membership that only worked with SQL Server.

The same concept is true for RoleManager and RoleStore.

2
  • Can you share link or name of the tutorial? Commented Aug 14, 2019 at 12:34
  • I don't remember the exact video. You can find it in Channel 9
    – Sasan
    Commented Aug 14, 2019 at 15:01
5

Identity is based on two primary blocks in ASP.NET Identity. There is an authentication manager which takes on the form of the UserManager<T> class. There is also the store manager which is an instance of UserStore<T>.

Difference?

The UserStore<T> object is injected into authentication manager which is used to identify and authenticate the UserStore<T> identity. The UserManager<T> reference acts as the authenticator for the UserStore<T> identity.

Important Details

ASP.NET Identity is based on the newest Open Web Interface. This means that that [typically], the IAuthenticationManager interface as declared in Microsoft.Owin.Security: Linked here, the authenticator injected into the UserManager class and controller and basically every operation that involves an authentication step.

In the following:

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
    var identity = await UserManager.CreateIdentityAsync(user,
    DefaultAuthenticationTypes.ApplicationCookie);
    AuthenticationManager.SignIn(new AuthenticationProperties() {
    IsPersistent = isPersistent }, identity);
}

We can notice the UserManager authentication manager being used to check the "UserStore" application user against the identity of the authentication manager. Snippet taken from blog.

Conclusion The UserManager is the essentially the domain logic of your ASP.NET Identity. The controller you use to handle a "login" or "identification" is passed in by a UserStore, which as MSDN: Userstore depicts...

Represents an Entity Framework implementation of a user store that supports IUserStore, IUserLoginStore, IUserClaimStore and IUserRoleStore.

Once the UserStore has all necessary fields to be qualified as an identity and you have qualified the UserManager as the authentication manager, and a method of storing the context...aka IdentityDbContext or some other method of storing the values in SQL (If used), you will have a Identity system capable of supporting logins.

2
  • It still confused me. This is my understanding: to implement operations against Users, UserManager does it on UserStore object. Main purpose of UserStore is to retrieve a collection of users. So why dont we just leave all operations (methods) in only UseManager class? What is the difference between methods(add, delete...) in UserStore and UserManaer?
    – MinhNguyen
    Commented Jul 9, 2015 at 15:32
  • 3
    The methods are different between the classes? They have the same name but one add's or deletes a claim to a user, the other adds or deletes the users claim from the authentication "collection"
    – Adam
    Commented Jul 9, 2015 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.