我想知道如何使用 Ninject 将 IPrincipal 绑定到 Asp.net Mvc 中的 HttpContext.Current.User 。
友好的问候,
泡菜
编辑:
不确定这是否重要,但我使用自己的 CustomPrincipal 类。
我想知道如何使用 Ninject 将 IPrincipal 绑定到 Asp.net Mvc 中的 HttpContext.Current.User 。
友好的问候,
泡菜
编辑:
不确定这是否重要,但我使用自己的 CustomPrincipal 类。
您可以在不需要提供者的情况下执行此操作NinjectModule
:
Bind<IPrincipal>()
.ToMethod(ctx => HttpContext.Current.User)
.InRequestScope();
请注意,我包含.InRequestScope()
以确保每个 HTTP 请求都缓存一次方法的值。即使您使用提供者机制,我也建议您这样做。
想我明白了:
public class PrincipalProvider : IProvider
{
public object Create(IContext context)
{
return HttpContext.Current.User;
}
public System.Type Type
{
get { return typeof(CustomPrincipal); }
}
}
在我的 NinjectModule 中,我这样做:
Bind<IPrincipal>().ToProvider<PrincipalProvider>();
如果这是错误的或不完整的,请告诉我,我会调整/删除。