简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM