The difference of member User in ComponentView and ControllerBase

Anders Jansson 21 Reputation points
2022-11-01T16:03:36.357+00:00

Inside VS 2022, I press F12 on the object "User" in a class DisplayAdminMenuViewComponent : ViewComponent and I get this

// Summary:
// Gets the System.Security.Principal.IPrincipal for the current user.

Doing the same thing but in a class derived from AdminController : Controller I get this.

// Summary:
// Gets the System.Security.Claims.ClaimsPrincipal for user associated with the
// executing action.

And operation User.IsInRole("Admin") fails for the ViewComponent base class, but works for Controller base class!

Why does the two base classes return a different implementation for object User???

Using Dot.Net Core 6.0

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,393 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,638 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 61,491 Reputation points
    2022-11-01T18:35:30.117+00:00

    a ViewComponent has both

    public System.Security.Principal.IPrincipal User { get; }
    public System.Security.Claims.ClaimsPrincipal UserClaimsPrincipal { get; }

    and a Controller only has:

    public System.Security.Claims.ClaimsPrincipal User { get; }

    note: a ClaimsPrincipal is an IPrincipal implementation that included claims, so any ClaimsPrincipal is an IPrincipal. old asp.net 4.5* standardized on ClaimsPrincipal and asp.net core controllers followed this. Razor page simple authentication does not require claims, so it supports both.

    0 comments No comments

0 additional answers

Sort by: Most helpful