c# - 在 ASP.NET MVC 3 中注入(inject) IPrincipal - 我做错了什么?

标签 c# asp.net-mvc-3 razor ninject

我有一个自定义的 IIdentity 实现:

public class FeedbkIdentity : IIdentity
{
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string Name { get; set; }

    public FeedbkIdentity() 
    {
        // Empty contructor for deserialization
    }

    public FeedbkIdentity(string name)
    {
        this.Name = name;
    }

    public string AuthenticationType
    {
        get { return "Custom"; }
    }

    public bool IsAuthenticated
    {
        get { return !string.IsNullOrEmpty(this.Name); }
    }
}

和自定义 IPrincipal

public class FeedbkPrincipal : IPrincipal
{
    public IIdentity Identity { get; private set; }

    public FeedbkPrincipal(FeedbkIdentity customIdentity)
    {
        this.Identity = customIdentity;
    }

    public bool IsInRole(string role)
    {
        return true;
    }
}

在 global.asax.cs 中,我正在反序列化 FormsAuthenticationTicket userData 并替换

HttpContext.Current.User:

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];

    if (authCookie != null)
    {
        FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

        JavaScriptSerializer serializer = new JavaScriptSerializer();

        FeedbkIdentity identity = serializer.Deserialize<FeedbkIdentity>(authTicket.UserData);

        FeedbkPrincipal newUser = new FeedbkPrincipal(identity);
        HttpContext.Current.User = newUser;
    }
}

然后,在 Razor Views 中我可以:

@(((User as FeedbkPrincipal).Identity as FeedbkIdentity).FirstName) 

我已经在使用 Ninject 为成员(member)提供商注入(inject)自定义用户存储库,并且我一直在尝试将 IPrincipal 绑定(bind)到 HttpContext.Current.User:

internal class NinjectBindings : NinjectModule
{
    public override void Load()
    {
        Bind<IUserRepository>().To<EFUserRepository>();
        Bind<IPrincipal>().ToMethod(ctx => ctx.Kernel.Get<RequestContext>().HttpContext.User).InRequestScope();
    }
}

但它不起作用。

我想要做的就是能够像这样访问我的自定义 IIdentity 属性: @User.Identity.FirstName

我怎样才能使这项工作?

编辑

我期望按照此处的建议将 IPrincipal 绑定(bind)到 HttpContext.Current.User:MVC3 + Ninject: What is the proper way to inject the User IPrincipal?我将能够在我的应用程序中访问 @User.Identity.FirstName

如果不是这种情况,如链接问题所示,将 IPrincipal 绑定(bind)到 HttpContext.Current.User 的目的是什么?

最佳答案

我建议创建您自己的基础 WebViewPage。参见 http://haacked.com/archive/2011/02/21/changing-base-type-of-a-razor-view.aspx

然后你可以在里面做

public FeedbkPrincipal FeedbkPrincipal
{
    get
    {
        return User as FeedbkPrincipal;
    }
}

public FeedbkIdentity FeedbkIdentity
{
     get
     {
         return FeedbkPrincipal.Identity as FeedbkIdentity;
     }
}

那就是

@FeedbkIdentity.FirstName

在 View 中。

what is the purpose of binding IPrincipal to HttpContext.Current.User as shown in the linked question

依赖注入(inject)让您可以在运行时选择您的组件。在给出的示例中,您可以这样做

public MyClassConstructor(IPrincipal principal) { // }

IPrincipal 将自动绑定(bind)到用户。请注意,这不会让您访问特定的 FeedbkPrincipal 属性,因为即使在运行时它会变成 FeedbkPrincipal 类型,您的类也不知道这一点。依赖注入(inject)不是解决当前问题的方法,因为您确实需要一种访问具体类的方法。

关于c# - 在 ASP.NET MVC 3 中注入(inject) IPrincipal - 我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10507937/

相关文章:

c# - ASP.Net 用一个 Controller 创建两个模型

c# - 拆分字符串或 XML 以将 ID 发送到存储过程?

Razor View Engine 子页面中的 Javascript Intellisense

c# - 使用动态表达式 API 时验证 ASP.NET MVC 3 Controller 的操作参数

c# - 继承和强类型 View

css - MVC Razor 显示标签

c# - 如何在不使用属性路由在 Route 属性上指定名称的情况下生成 Web Api 2 URL?

c# - 如何复制 Win8 metro File Picker UI

c# - ASP.NET MVC3 Entity Framework - 指定为此 MSL 一部分的 EntitySet 'x' 在 MetadataWorkspace 中不存在

css - Bootstrap Intellisense 缺少 Visual Studio 2015