Injecting IPrincipal in ASP.NET MVC 3 - What am I doing wrong?

asp.net-mvc-3, c#, ninject, razor

Solution

I'd recommend creating your own base WebViewPage. See http://haacked.com/archive/2011/02/21/changing-base-type-of-a-razor-view.aspx

Then inside that you could do

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

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

Then it's just

@FeedbkIdentity.FirstName

within the view.

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

Dependency injection lets you choose your components at run time. In the example given, you could do

public MyClassConstructor(IPrincipal principal) { // }

and IPrincipal would automatically be bound to the User. Note this would not let you access your specific `FeedbkPrincipal` properties because even though at runtime it would become of type `FeedbkPrincipal`, your class doesn't know that. Dependency injection isn't a solution to your current issue because you do need a way of accessing your concrete classes.

Problem

I've got a custom IIdentity implementation: ``` 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); } } } ``` and custom 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; } } ``` In global.asax.cs I'm deserializing FormsAuthenticationTicket userData and replacing 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; } } ``` Then, in Razor Views I can do: ``` @(((User as FeedbkPrincipal).Identity as FeedbkIdentity).FirstName) ``` I'm already using Ninject to inject custom User Repository for the membership provider and I've been trying to bind IPrincipal to 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(); } } ``` but it doesn't work. All I'm trying to do is to be able to access my custom IIdentity properties like this: `@User.Identity.FirstName` How can I make this work? EDIT I was expecting that by binding IPrincipal to HttpContext.Current.User as suggested here: MVC3 + Ninject: What is the proper way to inject the User IPrincipal? I will be able to access `@User.Identity.FirstName` in my application. If this is not the case, what is the purpose of binding IPrincipal to HttpContext.Current.User as shown in the linked question?

Original source

Related problems