Update
See Update below.
I have asked a previous stackoverflow question very recently, but I face an issue after implementing the suggested solutions from the following 2 places Identity model customization in ASP.NET Core and Custom storage providers for ASP.NET Core Identity. I am just not able to get the solution to work. I have implemented the extension of IdentityUser
as recommended and the extension of IdentityRole
. I have implemented IUserPasswordStore
and IUserRoleStore
and IUserStore
for UserStore
and I have implemented IRoleStore
for RoleStore
. I have also implemented the new dbContext ApplicationDbContext
which implements IdentityDbContext
. There was an issue with this with the constructor not accepting a parameter so I implemented a new constructor. I am not sure if this is right.
However that appears to be irrelevant because I get the error in the Main method of Program.cs when calling
CreateHostBuilder(args).Build().Run();
And the error is massive. I have placed it at the end. Searching the parts of the error online does not provide any ideas as to what I am doing, and as I am new to ASP.NET Core 3 and Identity I am stumped.
Here is the code I have so far.
ConfigureServices method
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddMvc(options =>
{
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
options.Filters.Add(new AuthorizeFilter(policy));
options.EnableEndpointRouting = false;
});
services.AddDbContext<EntitiesModel>(options => options.UseSqlServer(
Configuration["Data:ConnectionStrings:XXXXDbConnection"]));
services.AddIdentity<UserViewModel,RoleViewModel>().AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders().AddRoles<RoleViewModel>(); ;
services.AddTransient<IUserStore<UserViewModel>, UserStore>();
services.AddTransient<IRoleStore<RoleViewModel>, RoleStore>();
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Login";
options.LogoutPath = "/Logout";
});
}
ApplicationDbContext - these are all in the same namespace
public class IdentityDbContext
: IdentityDbContext<IdentityUser, IdentityRole, string>
{
public IdentityDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
}
public class IdentityDbContext<TUser>
: IdentityDbContext<TUser, IdentityRole, string>
where TUser : IdentityUser
{
public IdentityDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
}
// Uses the built-in Identity types except with custom User and Role types
// The key type is defined by TKey
public class IdentityDbContext<TUser, TRole, TKey> : IdentityDbContext<
TUser, TRole, TKey, IdentityUserClaim<TKey>, IdentityUserRole<TKey>,
IdentityUserLogin<TKey>, IdentityRoleClaim<TKey>, IdentityUserToken<TKey>>
where TUser : IdentityUser<TKey>
where TRole : IdentityRole<TKey>
where TKey : IEquatable<TKey>
{
private DbContextOptions<ApplicationDbContext> options;
//private string nameOrConnectionString;
public IdentityDbContext(DbContextOptions<ApplicationDbContext> options)
{
this.options = options;
}
}
public abstract class IdentityDbContext<
TUser, TRole, TKey, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken>
: IdentityUserContext<TUser, TKey>
where TUser : IdentityUser<TKey>
where TRole : IdentityRole<TKey>
where TKey : IEquatable<TKey>
where TUserRole : IdentityUserRole<TKey>
{
}
public class ApplicationDbContext : IdentityDbContext<UserViewModel,RoleViewModel,int>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<UserViewModel>(b =>
{
b.HasMany(e => e.UserRoles)
.WithOne()
.HasForeignKey(ur => ur.Id)
.IsRequired();
});
modelBuilder.Entity<UserViewModel>(b =>
{
b.ToTable("T_CustomerContacts");
});
modelBuilder.Entity<RoleViewModel>(b =>
{
b.ToTable("T_LoginRoles");
});
modelBuilder.Entity<UserRoleViewModel>(b =>
{
b.ToTable("T_CustomerContactRole");
});
modelBuilder.Entity<RoleViewModel>(b =>
{
b.HasMany(e => e.UserRoles)
.WithOne(e => e.Role)
.HasForeignKey(ur => ur.RoleId)
.IsRequired();
});
}
}
UserViewModel
public class UserViewModel : IdentityUser<int>
{
UserRoleViewModel
public class UserRoleViewModel : IdentityUserRole<int>
{
RoleViewModel
public class RoleViewModel : IdentityRole<int>
{
RoleStore
public class RoleStore : IRoleStore<RoleViewModel>
{
UserStore
public class UserStore : IUserStore<UserViewModel>, IUserPasswordStore<UserViewModel>, IUserRoleStore<UserRoleViewModel>
{
I have to add that this is the final configuration I have tried. I have tried many combinations and solutions. Part of the error is as follows
An error occurred while starting the application. AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IUserStore
1[TrussCorp.CustomerPortal.Models.ViewModel.Identity.UserViewModel] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore
4[TrussCorp.CustomerPortal.Models.ViewModel.Identity.UserViewModel,TrussCorp.CustomerPortal.Models.ViewModel.Identity.RoleViewModel,TrussCorp.CustomerPortal.Models.ViewModel.Identity.ApplicationDbContext,System.Int32]': Unable to resolve service for type 'TrussCorp.CustomerPortal.Models.ViewModel.Identity.ApplicationDbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore4[TrussCorp.CustomerPortal.Models.ViewModel.Identity.UserViewModel,TrussCorp.CustomerPortal.Models.ViewModel.Identity.RoleViewModel,TrussCorp.CustomerPortal.Models.ViewModel.Identity.ApplicationDbContext,System.Int32]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IRoleStore
1[TrussCorp.CustomerPortal.Models.ViewModel.Identity.RoleViewModel] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore3[TrussCorp.CustomerPortal.Models.ViewModel.Identity.RoleViewModel,TrussCorp.CustomerPortal.Models.ViewModel.Identity.ApplicationDbContext,System.Int32]': Unable to resolve service for type 'TrussCorp.CustomerPortal.Models.ViewModel.Identity.ApplicationDbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore
3[TrussCorp.CustomerPortal.Models.ViewModel.Identity.RoleViewModel,TrussCorp.CustomerPortal.Models.ViewModel.Identity.ApplicationDbContext,System.Int32]'.) Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable serviceDescriptors, ServiceProviderOptions options)
Update
I am back to where I began with the previous question and but solved the above error by reversing the orders of the following
services.AddTransient<IUserStore<UserViewModel>, UserStore>();
services.AddTransient<IRoleStore<RoleViewModel>, TrussCorp.CustomerPortal.Identity.RoleStore>();
services.AddIdentity<UserViewModel,RoleViewModel>().AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders().AddRoles<RoleViewModel>();
But as stated now I am getting Store does not implement IUserRoleStore UserManager.GetUserRoleStore() error which has put me back to square one.
My advice to other Developers is if you can not find a similar problem on the internet, you are doing something silly and unique.
The solution was to change this
public class UserStore : IUserStore<UserViewModel>, IUserPasswordStore<UserViewModel>, IUserRoleStore<UserRoleViewModel>
{
to this
public class UserStore : IUserStore<UserViewModel>, IUserPasswordStore<UserViewModel>, IUserRoleStore<UserViewModel>
{
Can you try instead adding this in the top perhaps?
services.AddIdentity<UserViewModel,RoleViewModel>().AddUserStore<UserStore>().AddRoleStore<RoleStore>();
来源:https://stackoverflow.com/questions/58702588/error-in-service-iuserstore-being-created-in-identity-asp-net-core-3-mvc