Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. MVC Role users error instance

MVC Role users error instance

Scheduled Pinned Locked Moved ASP.NET
asp-netarchitecturehelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DPaul1994
    wrote on last edited by
    #1

    I created a few roles for users and I'm trying to assing a role to a specific user but I get this error:

    An exception of type 'System.NullReferenceException' occurred in Microsoft.Owin.Host.SystemWeb.dll but was not handled in user code

    Additional information: Object reference not set to an instance of an object.

    If there is a handler for this exception, the program may be safely continued.

    and

    Line 47: get
    Line 48: {
    Line 49: return _userManager ?? HttpContext.GetOwinContext().GetUserManager();
    Line 50: }
    Line 51: private set

    In AccountController I don't have anything for this option. This is the function from Controller that should assing the role:

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

    Where is the mistake?

    H 1 Reply Last reply
    0
    • D DPaul1994

      I created a few roles for users and I'm trying to assing a role to a specific user but I get this error:

      An exception of type 'System.NullReferenceException' occurred in Microsoft.Owin.Host.SystemWeb.dll but was not handled in user code

      Additional information: Object reference not set to an instance of an object.

      If there is a handler for this exception, the program may be safely continued.

      and

      Line 47: get
      Line 48: {
      Line 49: return _userManager ?? HttpContext.GetOwinContext().GetUserManager();
      Line 50: }
      Line 51: private set

      In AccountController I don't have anything for this option. This is the function from Controller that should assing the role:

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

      Where is the mistake?

      H Offline
      H Offline
      Herman T Instance
      wrote on last edited by
      #2

      Did you DEBUG your code? Where did the exception occur?

      _userManager ?? HttpContext.GetOwinContext().GetUserManager();

      What is _userManager AND HttpContext.GetOwinContext().GetUserManager() are NULL. What would then be the behaviour?

      In Word you can only store 2 bytes. That is why I use Writer.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups