MVC 5 自定义用户存储 [英] MVC 5 Custom UserStore

查看:25
本文介绍了MVC 5 自定义用户存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要构建一个自定义的 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.

我看到了两条很接近的信息:

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 - 其中似乎适用于早期版本:(
  2. MVC 5 IoC 和身份验证 - 与我需要的类似,但我需要查看一些代码:(
  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.

有谁知道任何可能对我有帮助的文档?或者,如果默认 UserStore 出现故障并且它的内部方法会很棒(我可以从那里进行调整).

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).

推荐答案

这其实并不难.

创建一个实现 IUserStore 接口的新类.

Create a new class that implements the IUserStore interface.

public class MyUserStore : IUserStore<User> { }

...然后实现它的方法.我认为有 5 或 6 个开始,处理创建/更新/删除/查找用户.如果您为您的用户实体使用实体框架,那么只需构造函数将它的一个实例注入您的用户存储实例.

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

然后,在接口方法中,只需委托给您的 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);
}

然后您可以实现许多其他可选接口来支持其他功能.例如,如果您希望您的 UserStore 能够为您处理密码加密,请实现 IUserPasswordStore:

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

...等其他接口,如 IUserRoleStore 等.

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

真正有用的是反编译工具,例如 .NET Reflector 或 ReSharper 的 Navigate To Decompiled Sources 功能.如果您反编译 Microsoft.AspNet.Identity.EntityFramework 库中的默认 UserStore,您可以看到他们是如何做到的.一些反编译的代码可能难以阅读,因为其中一些方法是异步的,但您应该了解其要点.

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.

这篇关于MVC 5 自定义用户存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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