ASP.NET Identity 中 UserStore 和 UserManager 的使用有什么区别? [英] What is the difference in the use of UserStore and UserManager in ASP.NET Identity?

查看:90
本文介绍了ASP.NET Identity 中 UserStore 和 UserManager 的使用有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 ASP.NET Identity 很陌生,如果这个问题看起来很愚蠢,请多多包涵.

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

当我阅读以下链接中 Microsoft 网站上 UserStoreUserManager 类的定义时,看起来这两个类都定义了围绕用户的操作(如添加、查找、删除和修改).

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).aspxhttps://msdn.microsoft.com/en-us/library/dn613290(v=vs.108).aspx

推荐答案

那里的事情相当复杂,本来可以更容易.

Things are quite complicated there and could have been easier.

UserManger 是 ... 管理器.它并没有真正与存储、数据库交互.这就是 UserStore 所做的.

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

事实上 UserManager 有一个 构造函数,需要一个 UserStore.

In fact UserManager has a constructor which needs a UserStore.

为什么您必须使用不同的对象来管理用户?嗯,主要原因是您可以决定不使用 EF 并创建自己的用户存储.

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.

当您尝试实现自己的存储提供程序时,事情会变得更加清晰.我做到了,我的代码可以从 github 下载.

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

这是UserManager.正如你所看到的,里面的东西并不多.只需几行代码即可配置验证器.

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 相反,相当大.在那个例子中,我实现了一些接口并覆盖了一些方法.如果您想自定义与数据库的交互和/或扩展您的类,这就是您要做的.

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.

您通常不会与 UserStore 交互,实际上它是隐藏的.您只需创建它并将其传递给 UserManager 并......忘记它.

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.

您始终可以自定义您的 UserManager 并公开 UserStore:

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; }

}

并且,也许,override 一些方法:

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" });

它会起作用.

在上面的类中,如您所见,我覆盖了 UserManager 中的 CreateAsync.
该方法调用 UserStore.CreateAsync(),实际上,您必须调用基本方法 CreateAsync:

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);
   }

如果您不这样做,例如,返回 null,则不会调用 UserStore.CreateAsync 并且不会创建用户.

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.

最后说得通.

我想了解这个框架如何工作的最好方法是尝试使用您自己的存储来定制/实现您的解决方案,并查看所有类如何相互交互.

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.

示例 项目 不与数据库交互,而是使用 json 存储.调试起来非常容易.试一试,事情会在某个时候更清楚.

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.

这篇关于ASP.NET Identity 中 UserStore 和 UserManager 的使用有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆