2

我正在尝试使用默认的 MVC6 模板升级 Asp.Net Identity 2“自定义存储提供程序”以与 Visual Studio 2015 中的 Identity 3 beta 4 一起使用。

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

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

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

 .AddDefaultTokenProviders();

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

在我的项目中被称为 MySQLDatabase.cs

我找不到任何文件。

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

运行默认 MVC6 模板并单击“注册”按钮时收到以下错误:

“InvalidOperationException:在尝试激活 'Bondii.Identity.MySQL.UserStore`1[Bondii.Identity.MySQL.IdentityUser]' 时,无法解析类型 'Bondii.Identity.MySQL.MySQLDatabase' 的服务。”

我的代码包括完整项目:

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 -提供者

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

4

1 回答 1

0

在您的 startup.css 中,您需要以下内容:

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

前:

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

            .AddDefaultTokenProviders();

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

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

于 2015-06-24T10:09:30.900 回答