1

My RoleController:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RoleAddToUser(string UserName, string RoleName)
{
    ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
    var account = new AccountController();

    account.UserManager.AddToRole(user.Id, RoleName);

    ViewBag.ResultMessage = "Role created successfully !";

    // prepopulat roles for the view dropdown
    var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
    ViewBag.Roles = list;

    return View("ManageUserRoles");
}

My AccountController:

[Authorize]
public class AccountController : Controller
{
    private ApplicationSignInManager _signInManager;
    private ApplicationUserManager _userManager;


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

On the last line above is where it breaks. I am just trying to add a user to a role.

Object reference not set to an instance of an object.

[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.HttpContextBaseExtensions.GetOwinEnvironment(HttpContextBase context) +34
System.Web.HttpContextBaseExtensions.GetOwinContext(HttpContextBase context) +50

Edit: I think the problem is deeper than any mentioned in the "possible duplicate of another question". I don't think I even have the right references working, even though I see them in references in Solution Explorer, try to re-add them, and try to use NugGet to import/update them.

Not sure how or why this is happening but somehow I'm not referencing the OwinContext which be the might cause in the trace returning an Object reference not set to an instance.

I don't even know how to explain things this MVC/Owin/EF/references is slowly making me miss MS Access, Visual Basic and DLL Hell.

JustJohn
  • 1,362
  • 2
  • 22
  • 44
  • It looks like `GetOwinContext` is returning null, can you confirm that? – DavidG Oct 28 '15 at 01:26
  • Hi DavidG, I'm not sure how to tell if GetOwinContext is returning null. I can't seem to add using Microsoft.AspNet.Identity.Core; to the top of the Account Controller. I get a red line under Core. I re-downloaded this library from Nuget and it says it is already installed. But I'm thinking their is stuff in there that I'm needing. – JustJohn Oct 28 '15 at 05:03
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – trailmax Oct 28 '15 at 10:51

1 Answers1

0

There might be some method to all this madness.

After leaving everything alone for the day I thought, "Well, Register and Forgot Password, and Login are working, why don't I just see if I can add a user to role by hand and then see if I can create my own code to add the user at login since I won't be doing much 'assigning user to role' afterwards?")

So thanks to Google and last but not least StackOverFlow and this link, I not only was able to Add User to Role when they registered, but I was able to copy and paste some of the code in right above the AddToRole line that was giving me the error. and . . . it worked! Here is the code I added. I changed a few things because Register created a new user, but AddToRole was using an existing user. So I didn't need new namespaces, just new knowledge from people that are a lot smarter than me that's for sure.

public ActionResult RoleAddToUser(string UserName, string RoleName)
{
   var context = new ApplicationDbContext();
   ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
    var account = new AccountController();

   // var user = new ApplicationUser { UserName = UserName, Email = UserName };
    //var result = await UserManager.CreateAsync(user, model.Password);

    var roleStore = new RoleStore<IdentityRole>(context);
    var roleManager = new RoleManager<IdentityRole>(roleStore);
    var userStore = new UserStore<ApplicationUser>(context);
    var userManager = new UserManager<ApplicationUser>(userStore);     

   userManager.AddToRole(user.Id, RoleName);

    ViewBag.ResultMessage = "Role created successfully !";
Community
  • 1
  • 1
JustJohn
  • 1,362
  • 2
  • 22
  • 44