Using the RoleManager

Mar 18, 2015 at 6:28 PM
Hi there,

I've been looking through the sample, but was unable to find any usage of the RoleManager. I've implemented a custom RoleManager and are trying to create it using the Create method like this:
public class ApplicationRoleManager : RoleManager<ApplicationRole>
    {
        public ApplicationRoleManager(IRoleStore<ApplicationRole, string> roleStore)
            : base(roleStore)
        {
        }
        public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
        {
            return new ApplicationRoleManager(new RoleStore<ApplicationRole>(context.Get<ApplicationDbContext>()));
        }
    }
And I am adding them like this:
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
However, it won't compile because the RoleStore is expecting an IdentityCloudContext and not an IdentityCloudContext<ApplicationUser>. Should I just create two different identity contexts in my setup like this:
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext(ApplicationRoleDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
And implement the ApplicationRoleStore like this:
public class ApplicationRoleManager : RoleManager<ApplicationRole>
    {
        public ApplicationRoleManager(IRoleStore<ApplicationRole, string> roleStore)
            : base(roleStore)
        {
        }
        public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
        {
            return new ApplicationRoleManager(new RoleStore<ApplicationRole>(context.Get<ApplicationRoleDbContext>()));
        }
    }
Or how do you suggest to implement this?

Thanks in advance,

Søren
Mar 18, 2015 at 6:47 PM
With this:
public class ApplicationRoleManager : RoleManager<ApplicationRole>
    {
        public ApplicationRoleManager(IRoleStore<ApplicationRole, string> roleStore)
            : base(roleStore)
        {
        }
        public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
        {
            return new ApplicationRoleManager(new RoleStore<ApplicationRole>());
        }
    }
I can create new roles and find existing roles, but I cannot return a RoleStore.Roles from my RolesController like this:
[Route("", Name = "GetAllRoles")]
        public IHttpActionResult GetAllRoles()
        {
            var roles = this.AppRoleManager.Roles;

            return Ok(roles);
        }
It gives my a NotSupportException ({"Specified method is not supported."}):
   at ElCamino.AspNet.Identity.Dynamo.RoleStore`3.get_Roles()
   at Microsoft.AspNet.Identity.RoleManager`2.get_Roles()
   at Controllers.RolesController.GetAllRoles() in RolesController.cs:line 31
   at lambda_method(Closure , Object , Object[] )
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
May 5, 2016 at 7:51 PM
I have an updated version of this project on Github (https://github.com/c0achmcguirk/AspNetIdentity_DynamoDB) and in NuGet at https://www.nuget.org/packages/ElCamino.AspNet.Identity.Dynamo.v2

I just updated a new version that does support RoleManager.Roles in an experimental fashion. Perhaps it will work for you?