I need to create my own classes to handle identity in EF Core 2.1. I have the following ones, but when wanting to inbocar a method of a controller I get the following error
Unable to solve service for type 'Sample.API.Models.Identity.Stores.ApplicationUserStore' while attempting to activate 'Sample.API.Models .Identity.Managers.ApplicationUserManager'.
public void ConfigureServices(IServiceCollection servicesCollection)
{
servicesCollection.AddDbContext<MyIndentityContext>(currentOptions =>
currentOptions.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
servicesCollection.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<MyIndentityContext>()
.AddRoleStore<ApplicationRoleStore>()
.AddUserStore<ApplicationUserStore>()
.AddUserManager<ApplicationUserManager>()
.AddRoleManager<ApplicationRoleManager>()
.AddSignInManager<ApplicationSignInManager>()
.AddDefaultTokenProviders();
servicesCollection.AddTransient<UserManager<ApplicationUser>, ApplicationUserManager>();
servicesCollection.AddTransient<SignInManager<ApplicationUser>, ApplicationSignInManager>();
servicesCollection.AddTransient<RoleManager<ApplicationRole>, ApplicationRoleManager>();
servicesCollection.AddTransient<IUserStore<ApplicationUser>, ApplicationUserStore>();
servicesCollection.AddTransient<IRoleStore<ApplicationRole>, ApplicationRoleStore>();
...
...
...
}
public class MyIndentityContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
{
private readonly IConfiguration _configuration;
private readonly IHttpContextAccessor _httpContextAccessor;
public MyIndentityContext(DbContextOptions dbContextOptions, IHttpContextAccessor httpContextAccessor,
IConfiguration configuration)
: base(dbContextOptions)
{
_configuration = configuration;
_httpContextAccessor = httpContextAccessor;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasDefaultSchema("Sample.API");
}
}
public class ApplicationRoleManager : RoleManager<ApplicationRole>
{
public ApplicationRoleManager(ApplicationRoleStore roleStore,
IEnumerable<IRoleValidator<ApplicationRole>> roleValidators, ILookupNormalizer keyNormalizer,
IdentityErrorDescriber errors, ILogger<ApplicationRoleManager> logger) : base(roleStore,
roleValidators,
keyNormalizer, errors, logger)
{
}
}
public class ApplicationSignInManager : SignInManager<ApplicationUser>
{
public ApplicationSignInManager(ApplicationUserManager userManager, IHttpContextAccessor contextAccessor,
IUserClaimsPrincipalFactory<ApplicationUser> claimsFactory, IOptions<IdentityOptions> optionsAccessor,
ILogger<ApplicationSignInManager> logger, IAuthenticationSchemeProvider schemes) : base(userManager,
contextAccessor, claimsFactory, optionsAccessor, logger, schemes)
{
}
}
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(ApplicationUserStore userStore, IOptions<IdentityOptions> optionsAccessor,
IPasswordHasher<ApplicationUser> passwordHasher,
IEnumerable<IUserValidator<ApplicationUser>> userValidators,
IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer,
IdentityErrorDescriber errors, IServiceProvider services, ILogger<ApplicationUserManager> logger) :
base(userStore, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors,
services, logger)
{
}
}
public class ApplicationRoleStore : RoleStore<ApplicationRole, MyIndentityContext>
{
public ApplicationRoleStore(MyIndentityContext dbContext, IdentityErrorDescriber identityErrorDescriber)
: base(dbContext, identityErrorDescriber)
{}
}
public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, MyIndentityContext, string>
{
public ApplicationUserStore(MyIndentityContext dbContext, IdentityErrorDescriber identityErrorDescriber)
: base(dbContext, identityErrorDescriber)
{}
}
public class ApplicationUser : IdentityUser {}
public class ApplicationRole : IdentityRole
{
public ApplicationRole() { }
public ApplicationRole(string roleName) : base(roleName) { }
public ApplicationRole(string roleName, string roleDescription) : base(roleName)
{
Description = roleDescription;
}
}
[Authorize]
[ApiController]
[Route("api/[controller]")]
[EnableCors(CORS.AllowSpecificOrigins)]
public class UserController : BaseController
{
private readonly ApplicationUserManager _applicationUserManager;
public UserController(ApplicationUserManager applicationUserManager)
{
_applicationUserManager = applicationUserManager;
}
// GET: api/User/5
[HttpGet("{id}")]
public async Task<UserDTO> Get(int id)
{
var currentuser = await _applicationUserManager.FindByNameAsync("NAME");
return ...;
}
}
EDIT:
After doing what the user "Camilo Terevinto" recommended, I made it work, but I also had to change the constructor of all the managers. For example I had to change ApplicationRoleStore roleStore by IRoleStore roleStore
When using Dependency Injection, the classes that have dependencies don't ask for the implementation, they ask for the contract.
This:
private readonly ApplicationUserManager _applicationUserManager;
public UserController(ApplicationUserManager applicationUserManager)
{
_applicationUserManager = applicationUserManager;
}
should be:
private readonly UserManager<ApplicationUser> _applicationUserManager;
public UserController(UserManager<ApplicationUser> applicationUserManager)
{
_applicationUserManager = applicationUserManager;
}
Also, notice that you don't need any of these lines, these are already added by the previous calls to .AddIdentity
and its options.
servicesCollection.AddTransient<UserManager<ApplicationUser>, ApplicationUserManager>();
servicesCollection.AddTransient<SignInManager<ApplicationUser>, ApplicationSignInManager>();
servicesCollection.AddTransient<RoleManager<ApplicationRole>, ApplicationRoleManager>();
servicesCollection.AddTransient<IUserStore<ApplicationUser>, ApplicationUserStore>();
servicesCollection.AddTransient<IRoleStore<ApplicationRole>, ApplicationRoleStore>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With