Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore violates the constraint of type parameter 'TUser'. #1082
Comments
When you are customizing ASP.NET Core Identity, you should not use public class ApplicationRoleManager: RoleManager<Role>
public class ApplicationRoleStore : RoleStore<Role, ApplicationDbContext, int, UserRole, RoleClaim>
public class ApplicationSignInManager : SignInManager<User>
public class ApplicationUserManager : UserManager<User>
public class ApplicationUserStore : UserStore<User, Role, ApplicationDbContext, int, UserClaim, UserRole, UserLogin, UserToken, RoleClaim> Plus your ApplicationRoleStore should provide how to create the RoleClaim, protected override RoleClaim CreateRoleClaim(Role role, Claim claim)
{
return new RoleClaim
{
RoleId = role.Id,
ClaimType = claim.Type,
ClaimValue = claim.Value
};
} And also the ApplicationUserStore should provide these mappings too: protected override UserClaim CreateUserClaim(User user, Claim claim)
{
var userClaim = new UserClaim { UserId = user.Id };
userClaim.InitializeFromClaim(claim);
return userClaim;
}
protected override UserLogin CreateUserLogin(User user, UserLoginInfo login)
{
return new UserLogin
{
UserId = user.Id,
ProviderKey = login.ProviderKey,
LoginProvider = login.LoginProvider,
ProviderDisplayName = login.ProviderDisplayName
};
}
protected override UserRole CreateUserRole(User user, Role role)
{
return new UserRole
{
UserId = user.Id,
RoleId = role.Id
};
}
protected override UserToken CreateUserToken(User user, string loginProvider, string name, string value)
{
return new UserToken
{
UserId = user.Id,
LoginProvider = loginProvider,
Name = name,
Value = value
};
} Then redirect built-in services to your custom services: services.AddScoped<UserStore<User, Role, ApplicationDbContext, int, UserClaim, UserRole, UserLogin, UserToken, RoleClaim>, ApplicationUserStore>();
services.AddScoped<UserManager<User>, ApplicationUserManager>();
services.AddScoped<RoleManager<Role>, ApplicationRoleManager>();
services.AddScoped<SignInManager<User>, ApplicationSignInManager>();
services.AddScoped<RoleStore<Role, ApplicationDbContext, int, UserRole, RoleClaim>, ApplicationRoleStore>();
services.AddScoped<IEmailSender, AuthMessageSender>();
services.AddScoped<ISmsSender, AuthMessageSender>(); now introduce your custom services: services.AddIdentity<User, Role>(identityOptions =>
{
// ...
}).AddUserStore<ApplicationUserStore>()
.AddUserManager<ApplicationUserManager>()
.AddRoleStore<ApplicationRoleStore>()
.AddRoleManager<ApplicationRoleManager>()
.AddSignInManager<ApplicationSignInManager>()
// You **cannot** use .AddEntityFrameworkStores() when you customize everything
//.AddEntityFrameworkStores<ApplicationDbContext, int>()
.AddDefaultTokenProviders(); |
In 2.0 AddEntityFrameworkStores was improved to make things easier, the TKey argument was removed and the DbContext is sufficient to 'do the right thing'. You don't need to actually register the ApplicationXyz services unless you are consuming those services in your own code. But AddUser/RoleStore is needed at a minimum for this to work. |
Thank you very much. |
@HaoK I Must be missing something for my UserStore ! |
@master-still |
@VahidN Thank you I am not sure what is wrong ! |
If it helps, this is my UserStore:
And this is my AppRole:
|
@MaklaCof https://github.com/master-still/test currently I am having problem while passing my AppRole to Userstore. |
Solved It ! Thank you all. |
@master-still What did you do to fix this? I am using 1.1 and see the same error you reported. My code looks almost exactly the same as yours. Thanks |
Having the same issue. @master-still care to update your test repo with functioning code? Current version gives quite a bunch of build errors. |
Had the same problem. My problem started when I have overriden the IdentityUser to |
I am trying to use Id as int (Identity). My version is 1.1
I override all Identity classes:
and my context is:
and startup.cs:
I found few web-pages describing this error and all suggest upgrading to 1.1.
Exceptions is raised when debugging in line
.AddEntityFrameworkStores<AppDbContext, int>()
My complete error displayed in page is:
What did I miss?
The text was updated successfully, but these errors were encountered: