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

问题描述

我需要建立一个自定义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. - 这似乎适用于早期版本:(


  2. - 这是类似什么,我需要,但我需要看一些code:(

  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. andMVC 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反射或ReSharper的的定位要反编译源功能的反编译工具。如果在Microsoft.AspNet.Identity.EntityFramework库编译默认UserStore,你可以排序的看看他们是如何做到的。一些反编译code会很难阅读,因为一些方法是异步的,但你应该得到它的要点。

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个自定义UserStore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 22:56