Callum Linington
Callum Linington

Reputation: 14417

AuthenticationManager HttpContext is Null

I'm trying to use this code in my controller:

IAccountManagementService<User> _service;
public AccountController(IAccountManagementService<User> service)
{
    _service = service;
    _service.AuthenticationManager = HttpContext.GetOwinContext().Authentication;
    _service.UserManager = new UserManager<User>(new UserStore<User>(new DefaultContext()));
}

And it's been slightly adapted from the origin boilerplate code from the template for ASP.NET MVC 5

Its being injected via Ninject, Ninject is using a dependency resolver and module loader:

var kernel = new StandardKernel(new ModuleLoader());

DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));

So I don't know if it is this messing up the context.

The service just has the original code abstracted out into it, the service looks like this:

IRepository _repository;
public AccountManagementService(IRepository repository)
{
    _repository = repository;
}

public UserManager<User> UserManager { get; set; }
public IAuthenticationManager AuthenticationManager { get; set; }
public RoleManager<IdentityRole> RoleManager { get; set; }

The original code looks something like this:

public AccountController()
            : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}

public AccountController(UserManager<ApplicationUser> userManager)
{
    UserManager = userManager;
}

public UserManager<ApplicationUser> UserManager { get; private set; }

private IAuthenticationManager AuthenticationManager
{
    get
    {
        return HttpContext.GetOwinContext().Authentication;
    }
}

I could probably inject IAuthenticationManager but haven't really looked into it properly yet.

I'm just not sure when I load up my site, I click login and it takes me to the constructor of the controller and points to the line starting: _service.AuthenticationManager and tells me that the HttpContext is null.

Upvotes: 0

Views: 1338

Answers (1)

Callum Linington
Callum Linington

Reputation: 14417

I fixed this by re-doing all of my Ninject dependency injection. I'm now following the convention for Ninjecting MVC 5

I added these bindings for the ASP.NET Identity:

//<----- ACCOUNT STUFF ----->\\
kernel.Bind<IUserStore<EbpUser>>().To<UserStore<EbpUser>>().WithConstructorArgument("context", context => kernel.Get<DefaultContext>());
kernel.Bind<IRoleStore<IdentityRole, string>>().To<RoleStore<IdentityRole, string, IdentityUserRole>>().WithConstructorArgument("context", context => kernel.Get<DefaultContext>());

To then get the HttpContext I binded the AuthenticationManager:

kernel.Bind<IAuthenticationManager>().ToMethod(c => HttpContext.Current.GetOwinContext().Authentication).InRequestScope();

Inside my AccountManagementService I just do this now:

IRepository _repository;
IAuthenticationManager _authManager;
UserManager<EbpUser> _userManager;
RoleManager<IdentityRole> _roleManager;

public AccountManagementService(IRepository repository, IUserStore<EbpUser> userStore, IRoleStore<IdentityRole, string> roleStore, IAuthenticationManager authManager)
{
    _repository = repository;
    _authManager = authManager;
    _userManager = new UserManager<EbpUser>(userStore);
    _roleManager = new RoleManager<IdentityRole>(roleStore);
}

So now with everything injected properly it looks a lot neater.

Upvotes: 1

Related Questions