Asp.net Mvc:Ninject - IPrincipal

发布于 2024-08-30 19:34:44 字数 171 浏览 18 评论 0原文

我想知道如何使用 Ninject 将 IPrincipal 绑定到 Asp.net Mvc 中的 HttpContext.Current.User 。

友好的问候,

Pickels

编辑:

不确定这是否重要,但我使用我自己的 CustomPrincipal 类。

I was wondering how I could bind the IPrincipal to HttpContext.Current.User in Asp.net Mvc with Ninject.

Friendly greetings,

Pickels

Edit:

Not sure if it matters but I use my own CustomPrincipal class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠书 2024-09-06 19:34:44

您无需在 NinjectModule 中使用提供程序即可执行此操作:

Bind<IPrincipal>()
  .ToMethod(ctx => HttpContext.Current.User)
  .InRequestScope();

注意,我添加了 .InRequestScope() 以确保每个 HTTP 请求都会缓存该方法的值一次。即使您使用提供程序机制,我也建议这样做。

You can do this without the need for a provider in your NinjectModule:

Bind<IPrincipal>()
  .ToMethod(ctx => HttpContext.Current.User)
  .InRequestScope();

Note, I included .InRequestScope() to ensure that the value of the method is cached once per HTTP request. I'd recommend doing so even if you use the provider mechanism.

泅人 2024-09-06 19:34:44

我想我明白了:

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>();

如果这是错误或不完整的,请告诉我,我会调整/删除。

Think I got it:

public class PrincipalProvider : IProvider
{
    public object Create(IContext context)
    {
        return HttpContext.Current.User;
    }

    public System.Type Type
    {
        get { return typeof(CustomPrincipal); }
    }
}

And in my NinjectModule I do:

Bind<IPrincipal>().ToProvider<PrincipalProvider>();

If this is wrong or not complete please let me know and I'll adjust/delete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文