Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind IAuthenticationManager with Ninject in ASP.NET MVC 5?

I'm trying to bind IAuthenticationManager with Ninject so it can be injected into my AuthenticationService. The problem is that I currently get the IAuthenticationManager from HttpContext.GetOwinContext() on my Controller, like so:

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

How do I go about creating the binding with Ninject so that it knows to look for IAuthenticationManager from the HttpContext.GetOwinContext() at runtime? Is it possible? Does my question even make sense? Thanks in advance!

like image 572
Gup3rSuR4c Avatar asked Mar 23 '14 02:03

Gup3rSuR4c


People also ask

How do I add Windows authentication to an existing MVC project?

By default MVC apps use Form Authentication and Simple Membership, so you need to make it "false" to run Windows Authentication. Select the project name in Solution Explorer and then in the Property Explorer, click to enable Windows Authentication.

How to install the required dependencies for ninject in an MVC application?

This installs the required dependencies for Ninject in an MVC application. Step 1: Open Global.asax and change the subclass from System.Web.HttpApplication to Ninject.Web.Common.NinjectHttpApplication Step 2: Override the ‘CreateKernel’ method from the NinjectHttpApplication class in Global.asax and add the following

How to create a MVC 4 web application using mvcninject?

In this, select “ASP.NET MVC 4 Web Application. ” After selecting, just name your project as “MvcNInject” and finally click the OK button to create the project. Figure 2. Selecting Template After clicking OK button, another project template selection wizard will pop up with the name “New ASP.NET MVC 4 Project.”

What is ninject in MVC?

Today, we will look at Ninject (an IoC container) in a sample ASP.NET MVC Web application and understand the basics of DI using IoC. This article is published from the DNC Magazine – A Free High Quality Digital Magazine for .NET techies published once every two months.

How do I create a ninject http application?

Step 1: Open Global.asax and change the subclass from System.Web.HttpApplication to Ninject.Web.Common.NinjectHttpApplication Step 2: Override the ‘CreateKernel’ method from the NinjectHttpApplication class in Global.asax and add the following Step 3: Mapping the Dependencies.


1 Answers

So, I figured it out. Ninject provides access to the HttpContext directly, so I did this:

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

For anyone curious, here it is.

Update for @Meep

So, Ninject doesn't have to live in the same project as MVC. For that purpose I pulled it out into a separate project, in my case called "X.Dependencies". It references all other projects, NuGet packages, etc. that I need to actually set my bindings. It contains two files, the original C# file Ninject creates when added which I've renamed to NinjectConfiguration, and a cheaty file called AssemblyReferences which is required to make Visual Studio actually import all assemblies into the main project. Here's the code for it:

/// <summary>
/// Cheaty way to force Visual Studio to find all assembly references, even the ones not directly used by the main project.
/// </summary>
internal static class AssemblyReferences {
    internal static readonly Type t1 = typeof(Ninject.Web.Mvc.MvcModule);
}

Now, I suppose this could be avoided, but it's worked for me so far. I'm open to suggestions though. I just add a reference to it from my MVC project and let the WebActivator take care of initializing it, just as it had using the regular way.

I've also pulled out Owin into its' own project named "X.Owin" and it contains the usual Owin startup class, which I've simply renamed to OwinConfiguration.

Both of these are part of my "Domain Layer" which also contains a couple of other helper projects. One other noteworthy project from the list would be my "X.Mappings" which is for configuring AutoMapper mappings. It also uses the WebActivator to self initialize so I just add a reference to it in the MVC project.

Since I've pulled out a lot of code from the MVC project all it does at this point is basically routing and view rendering. Everything else is passed onto the helper projects as needed.

like image 50
Gup3rSuR4c Avatar answered Sep 19 '22 13:09

Gup3rSuR4c