Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspNetCore.Identity not working with custom User/Role implementation

Because I tend to favour Guid as my primary key type, my User and Role classes are implemented as follows

public class User : IdentityUser<Guid, UserClaim, UserRole, UserLogin>
{
}

public class Role : IdentityRole<Guid, UserRole, RoleClaim>
{
}

Note that UserClaim, UserRole, UserLogin & RoleClaim are all implemented in the same manner

Here is my DbContext implementation

public class ApplicationDbContext : IdentityDbContext<User, Role, Guid, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>
{
}

All good so far, except AspNetCore's new DI container doesn't seem to like my custom implementation by default. The following line of code, from my Startup.cs file throws the error shown below

services
    .AddIdentity<User, Role>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

GenericArguments[0], 'NewCo.DomainModel.Models.Identity.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type 'TUser'.

I am assuming that this is because the default Identity gremlin is implemented to use IdentityUser<string>, where I'm using IdentityUser<Guid>.

What do I do next? (I'm all outta ideas)

Note: I'm building against Microsoft's official .NET Core and ASP.NET Core releases as of Monday (June 27th, 2016) and Visual Studio Update 3 (if that's of any help to anyone)

like image 948
Matthew Layton Avatar asked Dec 18 '22 15:12

Matthew Layton


1 Answers

Since you're using a custom key type, you must specify it when calling AddEntityFrameworkStores:

services
    .AddIdentity<User, Role>()
    .AddEntityFrameworkStores<ApplicationDbContext, Guid>()
    .AddDefaultTokenProviders();
like image 196
Kévin Chalet Avatar answered Dec 24 '22 01:12

Kévin Chalet