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?