Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Am I missing some wiring to support RoleManager? #57

Open
famschopman opened this issue Jul 2, 2024 · 0 comments
Open

Am I missing some wiring to support RoleManager? #57

famschopman opened this issue Jul 2, 2024 · 0 comments

Comments

@famschopman
Copy link

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;
	}
   
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant