Injecting an IPrincipal with Unity Property Injection at runtime

Using Unity in an ASP.Net MVC 2 app I have various dependencies on Controllers instantiated correctly. However, I want to ensure that the current IPrincipal for the user is going to be passed via injection to lower level Services, Repository etc.

Therefore in a lower level service I have something like:

[Dependency] IPrincipal CurrentUser {get; set;}

If I use Property Dependency Injection I do not get what I want because the Controller is instantiated BEFORE a User principal is available and in any case Unity does not know to get the current user credentials.

So what I want is to be able to inject the current user's IPrincipal (or probably RolePrincipal) into one of the dependencies for the Controller.

How can I do this?

-------------Problems Reply------------

Why not take the direct route, and just assign it.

Thread.CurrentPrincipal = user;

Dependency injection is good, but don't let it get in the way of the best dependency injector, the programmer.

While this thread is old, it looks like Jon Kruger has an answer that seems to directly answer the original question: http://jonkruger.com/blog/2009/04/13/hiding-threadcurrentprincipal-from-your-code/

Why inject it? The current principal is already present as User. That is what we use, and it works fine so far. The user shouldn't change within a single request, should it?

protected void Application_AuthenticateRequest()
{
var ticket = GetAuthenticationTicket();
// Perform actual authentication, etc.
MyUser user = BigAuthStuff();
Context.User = user;
Thread.CurrentPrincipal = user;
}

public class MyBaseController : Controller
{
protected MyUser AuthenticatedUser
{
get { return User as MyUser; }
}
}

Category:asp.net mvc Views:0 Time:2010-03-09

Related post

  • @Enterprise Library Unity property inject 2011-06-01

    I'm a fresher with the enterprise library. I want to ask some questions and any help will be appreciate. 1、 How to deploy inject an instance property. public class MyObject { public MyObject(string Title) { ///... } public MyObject(InjectObject injec

  • Property injection with Unity causing stack overflow 2010-06-05

    I have been using Unity for quite a while but I have always used it with constructor injection. In an effort to reduce the number of classes I have to inject into my view models (as my commands rely on them) I thought I would try creating a concept t

  • How to use property injection in unity and asp.net mvc3? 2011-10-03

    I would like to use property injection in an MVC3 application. I have configured Unity 2 as a DI container and everything works just fine by constructor injection but I can't figure out how to use property injection. I marked properties with the [Dep

  • Unity Inject parameter in constructor of property beeing resolved 2011-10-10

    I can't find a way to inject parameters in this scenario: class Top { private ISome some; public Top() { some = CreateSome(localRuntimeVariable); } //I need to pass "some" instance as a InjectionParameter to Child constructor [Dependency] public Chil

  • Unity Dependency Injection (constructor or property) 2012-01-22

    I've been using Unity for some time in my project. I have a singleton Container which I use to register types and/or instances, and later resolve. today i wanted to automate some of the instantiation by using property or constructor injection. I star

  • Why is Unity using property injection when resolving a concrete type? 2012-02-03

    I have an interface: public interface IFoo{ string Bar{ get; set; } } and an implementation of the interface public class RealFoo{ public string Bar{ get; set; } public string Qux{ get; set; } } I have configured unity, via the configuration file, to

  • How to perform property injection with Unity and asp.net mvc on action filters? 2009-04-10

    I am trying to get dependency injection working on my action filters with property injection. I can't figure out how to automatically set dependencies on the filters. Here is the code I have so far. public class UnityActionInvoker : ControllerActionI

  • Using Unity Framework, inject into System.Windows.Forms.Form page 2009-05-20

    I have a form: public partial class mdiAuthenticationForm : Form { public Services.Authentication.IAuthentication Authenticator { get; set; public Services.Authentication.IAuthorization Authorizor { get; set; } and I want to inject concrete classes f

  • Dependency Injection/Property Injection on an asp.NET MVC 2 ActionFilter: Help! 2011-04-08

    I've been trying to wrap my head around the topics posted at this similar question: Is it possible to use Dependency Injection/IoC on an ASP.NET MVC FilterAttribute ? However, I'm just not getting anywhere. Not to mention, all the solutions appear to

  • Constructor injection working- Property injection not so much 2009-06-25

    I am using Composite Application Block. I have a class that uses constructor injection (using the [ServiceDependency] attribute on the constructor parameter) and it's working. But when I try to switch to using property injection (with the [ServiceDep

  • Ninject 2 Property Injection for ActionFilterAttribute not working 2010-02-15

    I have a method attribute which expects several properties to be injected by Ninject 2, but userSession and jobRepository are coming up as null: [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public class JobAttribute : ActionFilter

  • property inject in Autofac 2010-04-21

    I am compiling: PropertyInject wiki page why this line: builder.RegisterType<A>().InjectProperties(); doesn't compile? how to do property inject in autofac? I am using vs2010, autofac 2.1.13.813. EDIT: after investigation, PropertyInjection sho

  • How do I get property injection working in Ninject for a ValidationAttribute in MVC? 2010-04-29

    I have a validation attribute set up where I need to hit the database to accomplish the validation. I tried setting up property injection the same way I do elsewhere in the project but it's not working. What step am I missing? public class Applicatio

  • Does Funq IoC Container support property injection? 2010-06-29

    I'm looking for an IoC container to use in my Compact Framework application. Trying out Funq I noticed that I can't find a way to do Property Injection with it. I've looked through the discussion on the project's site and the unit tests for it, but I

  • Property Injection in Base Controller using Ninject 2 2010-06-30

    I have the following code in my Global.aspx protected override void OnApplicationStarted() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); RegisterAllControllersIn(Assembly.GetExecutingAssembly()); } protected override IKern

  • Ninject 2.0: Property Injection without attribute 2010-07-12

    Is there a way to use Property Injection in Ninject 2 without using the [Inject] attribute? This creates a dependency to Ninject in the class that will be wired using it and I prefer to avoid having unneeded dependencies to my IoC container, that's w

  • Ninject property injection returns null 2010-08-24

    I've got a WinForms app with the following code: static void Main() { IKernel kernel = new StandardKernel(new MyModule()); TestInterface test = kernel.Get<TestInterface>(); } For the Module.Load() event: Bind<TestClass>().ToSelf().InSingl

  • Allow optional (null) property injection in Castle Windsor via factory method? 2010-08-26

    I've registered a component container.Register(Component.For<XType>() .LifeStyle.Is(LifestyleType.Transient) .UsingFactoryMethod(SomeMethod)); and am currently using this for property injection on my ASP.NET MVC controllers. However - my curren

  • HttpHandler Property Injection using Ninject returning null 2010-09-02

    I have the following httphandler: public class NewHandler : IHttpHandler { [Inject] public IFile FileReader { get; set; } public NewHandler() { } public void ProcessRequest(System.Web.HttpContext context) { .... var something = SomeMethod(FileReader)

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.153 (s). 11 q(s)