-
Notifications
You must be signed in to change notification settings - Fork 30
Open
Description
I wanted to manage the roles and created a basic routine in a controller
[ApiController]
[Route("[controller]")]
public class RolesController : RavenController
{
private readonly UserManager<AppUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
public RolesController(UserManager<AppUser> userManager, RoleManager<IdentityRole> roleManager, IAsyncDocumentSession ravenSession) : base(ravenSession)
{
_userManager = userManager;
_roleManager = roleManager;
}
[HttpPost]
public async Task<IActionResult> CreateRole([FromBody] CreateRoleDto dto)
{
if (string.IsNullOrEmpty(dto.RoleName))
{
return BadRequest("Role name is required");
}
var roleExist = await _roleManager.RoleExistsAsync(dto.RoleName);
if(roleExist)
{
return BadRequest("Role already exist");
}
var roleResult = await _roleManager.CreateAsync(new IdentityRole(dto.RoleName));
if (roleResult.Succeeded)
{
return Ok(new { message="Role created succesfully"});
}
return BadRequest("Role creation failed");
}
}
Quickly ending up with a "Unable to resolve service for type 'Microsoft.AspNetCore.Identity.RoleManager`1[Microsoft.AspNetCore.Identity.IdentityRole]' while attempting to activate 'SplitbyAPI.Controllers.RolesController'."
RavenDB.Identity is wired up, and from what I understood there are multiple versions of Identity but when providing both the Identity and Role it should also internally create the UserManager to be injected.
// Wiring up RavenDB
builder.Services.AddSingleton<IDocumentStoreFactory, DocumentStoreFactory>();
builder.Services.AddScoped<ITenantDocumentStore>(x =>
{
var tenantId = x.GetRequiredService<ITenantGetter>().Tenant;
return new TenantDocumentStore(tenantId, x.GetService<IDocumentStoreFactory>()!);
});
builder.Services.AddScoped<IAsyncDocumentSession>(s => {
return s.GetRequiredService<ITenantDocumentStore>().DocumentStore.OpenAsyncSession();
});
// Wiring up RavenDB Identity
builder.Services.AddIdentity<AppUser, Raven.Identity.IdentityRole>()
.AddRavenDbIdentityStores<AppUser, Raven.Identity.IdentityRole>();
Is there anything I should add to register the UserManager? I tried to extend the IdentityBuilder in Extensions by registering RoleMan to the RoleManager instance but this didn't seem to work either.
public static IdentityBuilder AddRavenDbIdentityStores<TUser, TRole>(this IdentityBuilder builder, Action<RavenDbIdentityOptions>? configure = null)
where TUser : IdentityUser
where TRole : IdentityRole, new()
{
if (configure != null)
{
builder.Services.Configure(configure);
}
builder.Services.AddScoped<IUserStore<TUser>, UserStore<TUser, TRole>>();
builder.Services.AddScoped<IRoleStore<TRole>, RoleStore<TRole>>();
builder.Services.AddScoped<RoleManager<TRole>, RoleMan<TRole>>();
return builder;
}
Metadata
Metadata
Assignees
Labels
No labels