Search code examples
c#asp.netasp.net-mvcasp.net-identity

Access Identity User from base controller constructor in ASP.NET 5


I tried to access the Identity current User in my abstract base controller constructor to use the link to display user in View if It signed in.

So, I got User property is null. Everybody advice use filter and return User via ViewData. Example here: "Why is User (as in User.Identity.Name) null in my abstract base controller?"

I tried do somethink like this, but face with the problem: AuthorizationContext has no member Controller!

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class UserAuthorizationAttribute : Attribute, IAuthorizationFilter
{
    public void OnAuthorization(Microsoft.AspNet.Mvc.Filters.AuthorizationContext filterContext)
    {
        filterContext.Controller.ViewData["IdentityUser"] = filterContext.HttpContext.User;
    }
}

So, I have two questions:

  • Is there any way to send data from Filter to the Controller?
  • Maybe there is any other way to get not null User?

Solution

  • You could try using an Action Filter as opposed to an Authorisation Filter. These are processed later in the pipeline, and have access to the user.