Using a Custom UserStore and RoleStore in ASP.NET 5
<p>I have implemented a custom RoleStore and a custom UserStore for my project that is using ASP.NET 5, MVC 6, EF 7, and Identity 3. However - I can't quite figure out how to configure identity to use my custom RoleStore and custom UserStore instead of the usual offering. How can I reconfigure the system to use my custom classes?</p>

<p>PS: I also have custom User and Role class.</p>

<h2>Solution</h2>

<p>Here's what I ended up doing. First, I uninstalled the 'Identity Entity Framework' package from my project. This sent a few things missing, so I re-implemented them (read: copied them from <a href="https://github.com/aspnet/Identity/tree/dev/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore" rel="nofollow">here</a>), and put them in a 'Standard' namespace to indicate they hadn't been customised. I now have a 'Security' namespace that contains the following:</p>

<ul>
<li>Standard

<ul>
<li>IdentityRole.cs</li>
<li>IdentityRoleClaim.cs</li>
<li>IdentityUser.cs</li>
<li>IdentityUserClaim.cs</li>
<li>IdentityUserLogin.cs</li>
<li>IdentityUserRole.cs</li>
</ul></li>
<li><strong>BuilderExtensions.cs</strong></li>
<li><strong>IdentityDbContext.cs</strong></li>
<li><strong>Resources.resx</strong></li>
<li><strong>Role.cs</strong></li>
<li><strong>RoleStore.cs</strong></li>
<li><strong>User.cs</strong></li>
<li><strong>UserStore.cs</strong></li>
</ul>

<p>The items shown in bold contain project specific functionality.</p>

<p>The code that allows me to use the custom stores is in the 'BuilderExtensions' file, which contains the following class:</p>

<pre><code>public static class BuilderExtensions
{
    public static IdentityBuilder AddCustomStores&lt;TContext, TKey&gt;(this IdentityBuilder builder)
        where TContext : DbContext
        where TKey : IEquatable&lt;TKey&gt;
    {
        builder.Services.TryAdd(GetDefaultServices(builder.UserType, builder.RoleType, typeof(TContext), typeof(TKey)));
        return builder;
    }

    private static IServiceCollection GetDefaultServices(Type userType, Type roleType, Type contextType, Type keyType)
    {
        var userStoreType = typeof(UserStore&lt;,,,&gt;).MakeGenericType(userType, roleType, contextType, keyType);
        var roleStoreType = typeof(RoleStore&lt;,,&gt;).MakeGenericType(roleType, contextType, keyType);
        var services = new ServiceCollection();
        services.AddScoped(
            typeof(IUserStore&lt;&gt;).MakeGenericType(userType),
            userStoreType);
        services.AddScoped(
            typeof(IRoleStore&lt;&gt;).MakeGenericType(roleType),
            roleStoreType);
        return services;
    }
}
</code></pre>

<p>This then allows me to write the following in my Startup.cs file:</p>

<pre><code>services.AddIdentity&lt;User, Role&gt;()
    .AddCustomStores&lt;PrimaryContext, string&gt;()
    .AddDefaultTokenProviders();
</code></pre>

<p>And the custom store will be used. Note that PrimaryContext is the name of my whole-project DbContext. it inherits from IdentityDbContext.</p>

<h2>Discussion</h2>

<p>I could have probably kept the 'Identity Entity Framework' package and saved myself duplicating the contents of the 'Standard' namespace, but I chose not to so that I can keep my identifiers short and unambiguous.</p>