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

925 views Asked by At

I am trying to upgrade an Asp.Net Identity 2 'custom storage provider' to work with Identity 3 beta 4 in Visual Studio 2015, with the default MVC6 template.

I'm having difficulty learning how to register the Asp.Net Identity 3 custom storage provider with ASP5 middleware.

At the moment I have done the following, but not sure if it is correct:

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

 .AddDefaultTokenProviders();

I'm not sure how I also register the database context?

In my project is is called MySQLDatabase.cs

I cannot find any documentation.

Registering Identity.EntityFramework middle-ware seems to be done differently to a custom storage provider.

I receive the following error when running the default MVC6 template and clicking the 'register' button:

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

My Code including full project:

GitHub repository to see my code is: https://github.com/simonpbond/bondii.identity.mysql

The original 'custom storage provider tutorial based on Identity 2 - I am trying to upgrade to Identity 3: http://www.asp.net/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider.

The original example code: https://aspnet.codeplex.com/SourceControl/latest#Samples/Identity/AspNet.Identity.MySQL/

1

There are 1 answers

0
Alex L On BEST ANSWER

In your startup.css you need something like:

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

Before:

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

            .AddDefaultTokenProviders();

This will give you a database connection per request i believe and will be passed to the constructor of your Stores by the DI.

This may not be the best way, it may be better to pass some sort of factory as a singleton but this should work the way you have it setup.