在Asp.Net Identtity中使用UserStore和UserManager有什么区别? [英] What is the difference in the use of UserStore and UserManager in Asp.Net Identtity?

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

问题描述

我对Asp.Net Identity还是很陌生,如果这个问题看起来很愚蠢,请忍受.因此,当我阅读下面链接中的Microsoft网站中的UserStore类和UserManager类的定义时,看起来这两个类都围绕用户定义了操作(例如添加",查找",删除"和修改").那么什么时候我可以一个使用另一个?:

I'm very new to Asp.Net Identity and please bear with me if this question seems silly. So, when I read the definition for UserStore class and UserManager class in Microsoft website which are in the links below, it looks like both class 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/zh- 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; }

}

也许还有ovveride的一些方法:

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 Identtity中使用UserStore和UserManager有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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