The difference of member User in ComponentView and ControllerBase

Anders Jansson 21 Reputation points

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

A set of technologies in the .NET Framework for building web applications and XML web services.
4,393 questions
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 ( 61,491 Reputation points

    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 4.5* standardized on ClaimsPrincipal and 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