Search code examples
asp.net-mvcasp.net-identity

ASP.NET Identity 2.0 RoleManager.Roles 'Object reference not set to an instance of an object.'


I am using ASP.NET Identity 2.0 with an ASP.NET MVC 5 and EF 6 project.

I am attempting to edit the roles associated with the users.

In my useradmin controller I have:

    //
    // GET: /Users/Edit/1
    public async Task<ActionResult> Edit(string id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        var user = await UserManager.FindByIdAsync(id);
        if (user == null)
        {
            return HttpNotFound();
        }

        var userRoles = await UserManager.GetRolesAsync(user.Id);

        return View(new EditUserViewModel()
        {
            Id = user.Id,
            Email = user.Email,
            RolesList = RoleManager.Roles.ToList().Select(x => new SelectListItem()
            {
                Selected = userRoles.Contains(x.Name),
                Text = x.Name,
                Value = x.Name
            })
        });
    }

I get the error

'Object reference not set to an instance of an object.'

at line:

return View(new EditUserViewModel()

When I try:

    //
    // GET: /Users/Edit/1
    public async Task<ActionResult> Edit(string id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        ViewBag.RoleId = new SelectList(RoleManager.Roles, "Id", "Name");

        var user = await UserManager.FindByIdAsync(id);
        if (user == null)
        {
            return HttpNotFound();
        }
        return View("EditUser", user);
    }

I get the error

'Object reference not set to an instance of an object.'

at line:

ViewBag.RoleId = new SelectList(RoleManager.Roles, "Id", "Name");

Am I missing a configuration setting?

At the beginning of the Controller I define:

    public UserManagementController(ApplicationUserManager userManager, ApplicationRoleManager roleManager)
    {
        UserManager = userManager;
        RoleManager = roleManager;
    }

    private ApplicationUserManager _userManager;
    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

    private ApplicationRoleManager _roleManager;
    public ApplicationRoleManager RoleManager
    {
        get
        {
            return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
        }
        private set
        {
            _roleManager = value;
        }
    }

Solution

  • The problem is, as we found out, that you've forgotten to create an instance of ApplicationRoleManager for each request. Add app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create); to App_Start/Startup.Auth.cs and you're good. :)