ASP.NET Identity 2.0 RoleManager ‘Object reference not set to an instance of an object’

Posted: March 23, 2016 in .Net/C#

I am trying the IdentitySamples in my own helloworld MVC project. I have added the controller, model and view codes, but when I tried to edit the user, it gives me the exception in the below line

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
            })
        });

When debugging, i found that RoleManager is null. To resolve it, I need add the below line in Startup.Auth.cs

app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s