在运行时使用Unity Property Injection注入IPrincipal

Red*_*ed1 2 asp.net-mvc dependency-injection unity-container

在ASP.Net MVC 2应用程序中使用Unity我对控制器的各种依赖关系正确实例化.但是,我想确保用户的当前IPrincipal将通过注入传递到较低级别的服务,存储库等.

因此,在较低级别的服务中,我有类似的东西:

[Dependency] IPrincipal CurrentUser {get; set;}
Run Code Online (Sandbox Code Playgroud)

如果我使用Property Dependency Injection,我得不到我想要的东西,因为Controller在用户主体可用之前被实例化,并且在任何情况下Unity都不知道获取当前用户凭据.

所以我想要的是能够将当前用户的IPrincipal(或可能是RolePrincipal)注入到Controller的一个依赖项中.

我怎样才能做到这一点?

cod*_*eim 7

为什么不采取直接路线,只需分配它.

Thread.CurrentPrincipal = user;

依赖注入是好的,但不要让它妨碍最好的依赖注入器,程序员.