如何使用 ASP-5 / MVC-6 / Identity 3 为自定义 Asp.Net 身份存储提供程序注册中间件

How to register middle-ware for a Custom Asp.Net Identity Storage Provider using ASP-5 / MVC-6 / Identity 3

我正在尝试升级 Asp.Net Identity 2 'custom storage provider' 以使用默认 MVC6 模板在 Visual Studio 2015 中使用 Identity 3 beta 4。

我很难学习如何使用 ASP5 中间件注册 Asp.Net Identity 3 自定义存储提供程序。

目前我做了以下操作,但不确定是否正确:

services.AddIdentity()
 .AddRoleStore>()
 .AddUserStore>()

 .AddDefaultTokenProviders();

我不确定我是如何注册数据库上下文的?

在我的项目中叫做MySQLDatabase.cs

我找不到任何文档。

注册 Identity.EntityFramework 中间件似乎与自定义存储提供程序不同。

当 运行 默认 MVC6 模板并单击 'register' 按钮时,我收到以下错误:

"InvalidOperationException: Unable to resolve service for type 'Bondii.Identity.MySQL.MySQLDatabase' while attempting to activate 'Bondii.Identity.MySQL.UserStore`1[Bondii.Identity.MySQL.IdentityUser]'."

我的代码包括完整项目:

GitHub仓库看我的代码是: https://github.com/simonpbond/bondii.identity.mysql

基于 Identity 2 的原始'自定义存储提供程序教程 - 我正在尝试升级到 Identity 3: http://www.asp.net/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider.

原示例代码: https://aspnet.codeplex.com/SourceControl/latest#Samples/Identity/AspNet.Identity.MySQL/

在你的startup.css中你需要这样的东西:

 services.AddTransient(_ => new MySQLDatabase());

之前:

 services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddRoleStore<MySQL.RoleStore<IdentityRole>>()
            .AddUserStore<MySQL.UserStore<IdentityUser>>()

            .AddDefaultTokenProviders();

我相信这将为您提供每个请求的数据库连接,并将由 DI 传递给您的 Stores 的构造函数。

这可能不是最好的方法,将某种工厂作为单例传递可能更好,但这应该按照您设置的方式工作。