asp.net-mvc - 在运行时使用 Unity 属性注入(inject)注入(inject) IPrincipal

标签 asp.net-mvc dependency-injection unity-container

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

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

[Dependency] IPrincipal CurrentUser {get; set;}

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

所以我想要的是能够将当前用户的 IPrincipal(或者可能是 RolePrincipal)注入(inject) Controller 的依赖项之一。

我该怎么做?

最佳答案

为什么不走直达路线,直接分配就可以了。

Thread.CurrentPrincipal = 用户;

依赖注入(inject)很好,但不要让它妨碍最好的依赖注入(inject)器,程序员。

关于asp.net-mvc - 在运行时使用 Unity 属性注入(inject)注入(inject) IPrincipal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2410927/

相关文章:

asp.net-mvc - ASP.NET MVC Textbox HtmlHelper 的 NullReferenceException 问题

ASP.NET MVC : how to prevent a session lock?

dependency-injection - 使用 ITypeConverter 进行 AutoMapper 依赖注入(inject)

c# - 将 IoC 容器作为依赖项注入(inject)有多糟糕?

asp.net-mvc - ASP.NET MVC - 在没有 Controller 的情况下显示 404 共享 View

ASP.NET Identity - 使用角色作为预定义的声明集

java - 为什么使用 Guice Provider 而不是普通注入(inject)

c# - 依赖倒置原理在ASP.NET Provider模型中的应用

inversion-of-control - 测试类型是否已在 Unity 中注册

c# - Unity(依赖注入(inject)): How to pass in a parameter to the constructor in RegisterType