Search code examples
c#asp.net-mvcasp.net-identityninject

I don't understand how create RoleManager by Ninject in my app, MVC


Please ask me, How add a binding in the Ninject.Web.Common?

I have the entity for the Role

public class AppRole : IdentityRole
{
    public AppRole() : base() { }

    public AppRole(string name)
        : base(name)
    { }
}

and the manager

public class AppRoleManager : RoleManager<AppRole>
{
    public AppRoleManager(RoleStore<AppRole> store) : base(store)
    { }
}

i want that the creating method are doing in Ninject like i did the UserManager and SignUpManager

my Ninject common

private static void RegisterServices(IKernel kernel)
    {
        System.Web.Mvc.DependencyResolver.SetResolver(new Infrastructure.NinjectDependencyResolver(kernel));

        kernel.Bind<IRoleStore<IdentityRole, string>>().To<RoleStore<IdentityRole, string, IdentityUserRole>>();
        kernel.Bind<RoleManager<IdentityRole, string>>().ToSelf();

    }

kernel.Bind<IRoleStore<IdentityRole, string>>().To<RoleStore<IdentityRole, string, IdentityUserRole>>();
    kernel.Bind<RoleManager<IdentityRole, string>>().ToSelf();

I change the binding which where wrong to

kernel.Bind<AppRoleManager>().ToSelf();
        kernel.Bind<IRoleStore<IdentityRole, string>>().To<RoleStore<IdentityRole, string, IdentityUserRole>>();

and the problem in constructor is off. (thank you Alexander)

but the problem in view

enter image description here

my controller Didn't have any error when i compile the app. However in return view I took it

private IUserManagerRepository userManager; private readonly AppRoleManager userRole;

    public RoleController(IUserManagerRepository userManager, AppRoleManager userRole)
    {
        this.userManager = userManager;
        this.userRole = userRole;
    }

    // GET: Role
    public ActionResult Index()
    {
        return View(userRole.Roles);
    }

And my view

@using Domain.IdentityManager
@using Domain.Entities
@model IEnumerable<AppRole>

<div class="panel panel-primary">
<div class="panel-heading">Roles</div>
<table class="table table-striped">
    <tr>
        <th>ID</th>
        <th>Название</th>
        <th>Пользователи</th>
        <th style="min-width: 150px"></th>
    </tr>
        @foreach (AppRole role in Model)
        {
            <tr>
                <td>@role.Id</td>
                <td>@role.Name</td>
                <td>
                    @if (role.Users == null || role.Users.Count == 0)
                    {
                        @: Нет пользователей в этой роли
                    }
                    else
                    {

                }
                </td>
        }

</table>


Solution

  • To fix this error you have to make consistent accessibility of RoleController constructor and its parameter type. Since constructor is public its parameters have to be public as well. Compiler complaining about AppRoleManager<IdentityRole> so we need to look at it. Apparently AppRoleManager is public and it looks like IdentityRole is only not public type here. So update its declaration

    public class IdentityRole //