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!
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.
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
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.”
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With