I have a regular 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() {
and user 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 deserialize the FormsAuthenticationTicket userData data and replace
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 already use Ninject to add a custom repository for the membership provider, and I tried to associate IPrincipal with 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 that will not work.
All I'm trying to do is access my iddentity user properties as follows: @User.Identity.FirstName
How can I do this job?
EDIT
I expected that when contacting IPrincipal with HttpContext.Current.User, as suggested here: MVC3 + Ninject: What is the correct way to enter 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 related question?