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

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() { // 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 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?

+4
source share
2 answers

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

Then inside, what could you do

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

Then it's just

 @FeedbkIdentity.FirstName 

in view.

What is the purpose of binding IPrincipal to HttpContext.Current.User, as shown in the related question

Enabling dependencies allows you to select components at runtime. In the above example, you can do

 public MyClassConstructor(IPrincipal principal) { // } 

and IPrincipal will be automatically bound to the user. Note that this will not allow you to access your specific FeedbkPrincipal properties, because although it will be of type FeedbkPrincipal , your class does not know this. Enabling dependencies is not a solution to your current problem, because you need a way to access your specific classes.

+3
source

Assuming that HttpContext.User installed correctly, you can create a global filter to automatically populate the ViewBag your FeedbkIdentity .

eg.

Create it

 public class FeedbkPrincipalIntoViewBagAttribute : ActionFilterAttribute { public override void OnActionExecuted(ActionExecutedContext filterContext) { var principal = filterContext.HttpContext.User as FeedbkPrincipal; if (principal != null) { filterContext.Controller.ViewBag.Identity = principal.Identity as FeedbkIdentity; } } } 

Register it

 protected void Application_Start() { GlobalFilters.Filters.Add(new FeedbkPrincipalIntoViewBagAttribute()); } 

Use it

 @ViewBag.Identity.FirstName 
+1
source

Source: https://habr.com/ru/post/1411413/


All Articles