简体   繁体   English

MVC 5自定义用户存储

[英]MVC 5 Custom UserStore

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. 我需要构建一个自定义UserStore(基本上因为我有一些默认情况下不属于框架的要求)但我无法找到任何关于如何执行此操作的文档。

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 :( http://kazimanzurrashid.com/posts/implementing-user-confirmation-and-password-reset-with-one-asp-dot-net-identity-pain-or-pleasure - 这似乎适用于早期版本:(
  2. and MVC 5 IoC and Authentication - Which is similar to what I require but I need to see some code :( MVC 5 IoC和身份验证 - 这与我的要求类似,但我需要看一些代码:(

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). 或者,如果默认的UserStore存在细分,那么它的内部方法会很好(我可以从那里进行调整)。

This is actually not that hard. 这实际上并不那么难。

Create a new class that implements the IUserStore interface. 创建一个实现IUserStore接口的新类。

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. 我认为有5或6个开始,处理创建/更新/删除/查找用户。 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: 然后,在接口方法中,只需委托给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: 例如,如果您希望UserStore能够为您处理密码加密,请实现IUserPasswordStore:

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

... and so on for the other interfaces like IUserRoleStore, etc. ...等等其他接口,如IUserRoleStore等。

What might really help is a decompilation tool like .NET Reflector or ReSharper's Navigate To Decompiled Sources feature. 可能真正有用的是反编译工具,如.NET Reflector或ReSharper的Navigate To Decompiled Sources功能。 If you decompile the default UserStore in the Microsoft.AspNet.Identity.EntityFramework library, you can sort of see how they did it. 如果您在Microsoft.AspNet.Identity.EntityFramework库中反编译默认的UserStore,您可以看看他们是如何做到的。 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. 一些反编译的代码可能难以阅读,因为有些方法是异步的,但你应该得到它的要点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC:控制器未捕获自定义Identity UserStore类中引发的异常 - ASP.NET MVC: Controller not catching exception thrown in custom Identity UserStore class 为身份UserStore注入自定义实现 - Inject Custom Implementation for Identity UserStore MVC var userStore =新的UserStore <ApplicationUser> (D b); 返回null - MVC var userStore = new UserStore<ApplicationUser>(db); returning null 如何模拟自定义 UserStore 和 RoleStore - How to mock custom UserStore and RoleStore 自定义 AspNet.Identity UserStore - Custom AspNet.Identity UserStore 如何将依赖项注入自定义Identity UserStore实现中? - How to injection dependencies into custom Identity UserStore implementation? 在 ASP.NET 5 中使用自定义 UserStore 和 RoleStore - Using a Custom UserStore and RoleStore in ASP.NET 5 UserManager.FindAsync不使用UserStore的自定义实现 - UserManager.FindAsync not working with custom implementation of UserStore 结合使用UserManager.FindAsync和自定义UserStore - Using UserManager.FindAsync with a custom UserStore 如何在 DI 中注册自定义 UserStore &amp; UserManager - How to register custom UserStore & UserManager in DI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM