title: MVC5 Account Controller null reference exception
body: <p>I am trying to implement user roles into my MVC web application. However I am getting a null exception on the line <code>return _userManager ?? HttpContext.GetOwinContext().GetUserManager&lt;ApplicationUserManager&gt;();</code> in my account controller. </p>
  
  <p>Account Controller</p>
  
  <pre><code>[Authorize]
  public class AccountController : Controller
  {
      private ApplicationSignInManager _signInManager;
      private ApplicationUserManager _userManager;
      private ApplicationRoleManager _roleManager; 
  
      public AccountController(){}
  
      public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, ApplicationRoleManager roleManager)
      {   UserManager = userManager;
          SignInManager = signInManager;
          RoleManager = roleManager; }
      public ApplicationRoleManager RoleManager
      {
          get { return _roleManager ?? HttpContext.GetOwinContext().Get&lt;ApplicationRoleManager&gt;(); }
          private set { _roleManager = value; }
      }
  
      public ApplicationSignInManager SignInManager
      {
          get { return _signInManager ?? HttpContext.GetOwinContext().Get&lt;ApplicationSignInManager&gt;(); }
          private set  { _signInManager = value; }
      }
  
      public ApplicationUserManager UserManager
      {
          get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager&lt;ApplicationUserManager&gt;(); }
          private set {_userManager = value; }
      }
  </code></pre>
  
  <p>ActionResult that will trigger the adding of the user to the role. </p>
  
  <pre><code>    [System.Web.Mvc.HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult RoleAddToUser(string UserName, string RoleName)
      {
          ApplicationUser user = context.Users.FirstOrDefault(u =&gt; u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase));
          var account = new AccountController();
          if (user != null) account.UserManager.AddToRole(user.Id, RoleName);
          var list = context.Roles.OrderBy(r =&gt; r.Name).ToList().Select(rr =&gt; new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
          ViewBag.Roles = list;
          return View("ManageUserRoles");
      }
  </code></pre>
  
  <p>Startup.Auth does contain </p>
  
  <pre><code>        app.CreatePerOwinContext(ApplicationDbContext.Create);
          app.CreatePerOwinContext&lt;ApplicationRoleManager&gt;(ApplicationRoleManager.Create);
          app.CreatePerOwinContext&lt;ApplicationUserManager&gt;(ApplicationUserManager.Create);
          app.CreatePerOwinContext&lt;ApplicationSignInManager&gt;(ApplicationSignInManager.Create);
  </code></pre>
  
  <p>Identity Config</p>
  
  <pre><code>    // Configure the application sign-in manager which is used in this application.
  public class ApplicationSignInManager : SignInManager&lt;ApplicationUser, string&gt;
  {
      public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
          : base(userManager, authenticationManager)
      {
      }
  
      public override Task&lt;ClaimsIdentity&gt; CreateUserIdentityAsync(ApplicationUser user)
      { return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager); }
  
      public static ApplicationSignInManager Create(IdentityFactoryOptions&lt;ApplicationSignInManager&gt; options, IOwinContext context)
      { return new ApplicationSignInManager(context.GetUserManager&lt;ApplicationUserManager&gt;(), context.Authentication); }
  }
  public class ApplicationRoleManager : RoleManager&lt;IdentityRole&gt;
  {
      public ApplicationRoleManager(IRoleStore&lt;IdentityRole, string&gt; roleStore)
          : base(roleStore) { }
  
      public static ApplicationRoleManager Create(IdentityFactoryOptions&lt;ApplicationRoleManager&gt; options, IOwinContext context)
      {
          return new ApplicationRoleManager(new RoleStore&lt;IdentityRole&gt;(context.Get&lt;ApplicationDbContext&gt;()));
      }
  }
  </code></pre>
  
  <p>Where is the null reference coming from? In the code I am double checking that the user exists. </p>
  
  <p>Thanks</p>