I was creating a couple of ViewComponents this evening in aspnet core and needed access to the
ClaimsPrincipal of the current logged in user. Like the MVC Controller I was expecting the
User property of the base class to be a
ClaimsPrincipal however I was wrong and it was only a
Was this a bug? Had I missed something? Did I really need to cast
User to being a
No, it was in fact an issue which had been raised in the aspnet Mvc Github repository a while back and fixed.
So where is it? It's in the
UserClaimsPrincipal property obviously!
/// <summary> /// Gets the <see cref="IPrincipal"/> for the current user. /// </summary> public IPrincipal User => ViewContext?.HttpContext?.User; /// <summary> /// Gets the <see cref="ClaimsPrincipal"/> for the current user. /// </summary> public ClaimsPrincipal UserClaimsPrincipal => ViewContext?.HttpContext?.User;
Any questions/comments then please contact me on Twitter @WestDiscGolf