How to custom/override User.IsInRole in ASP.NET Core

trungle.it

I'm a newbine in ASP.NET Core, I see in the User property (in ClaimsPrincipal class) in my controller, it has User.IsInRole method, so how can I override it to call my service dependency and register in my application (I don't want to use extension method).

Edward

For User.IsInRole, it is ClaimsPrincipal which is not registered as service, so, you could not replace ClaimsPrincipal, and you could not override IsInRole.

For a workaround, if you would not use extension method, you could try to implement your own ClaimsPrincipal and Controller.

  • CustomClaimsPrincipal which is inherited from ClaimsPrincipal

    public class CustomClaimsPrincipal: ClaimsPrincipal
    {
    
    public CustomClaimsPrincipal(IPrincipal principal):base(principal)
    {
    
    }
    
    public override bool IsInRole(string role)
    {
        return base.IsInRole(role);
    }
    }
    
  • ControllerBase to change ClaimsPrincipal User to CustomClaimsPrincipal User

    public class ControllerBase: Controller
    {
        public new CustomClaimsPrincipal User => new CustomClaimsPrincipal(base.User);
    }
    
  • Change the Controller from inheriting ControllerBase.

    public class HomeController : ControllerBase
    {
           public IActionResult About()
    {
        ViewData["Message"] = "Your application description page.";
        var result = User.IsInRole("Admin");
    
        return View();
    }
    
  • Change the logic in public override bool IsInRole(string role) based on your requirement

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET Core Custom Role Based Authorization (Custom User.IsInRole)?

ASP.NET Core Identity 2: User.IsInRole always returns false

User.IsInRole returns nothing in ASP.NET Core (Repository Pattern implemented)

Mock My.User.IsInRole() error in Asp.Net MVC

Asp.NET Identity Core IsInRole InvalidOperationException: Sequence contains more than one element

How to check if a user is authenticated in ASP.NET Core

ASP .NET Core: How to cache data which depend on user input?

ASP.NET Core how to redirect user to a page depending on a boolean

how to get claims of another user using ASP.NET Core

How to sign out other user in ASP.NET Core Identity

How to get current user in asp.net core

asp.net core how to add claims to User

How to get the current logged in user Id in ASP.NET Core

How to create user with ASP.NET Core 2.2

How get current login user in ViewComponent Asp.Net Core

How to get Asp.net Core Identity User in View

How update view depending on user selection on Asp.Net core

How to get the current user of the Ubuntu system in Asp .Net core 2.1?

ASP.Net Core API: How to link User Identity to a Contact

How to use user related data at Layout in Asp.Net Core?

How to check if a logged in user is in a role in ASP.NET Core

How do I update user claims in Asp.net Core

How to use User.IsInRole() in an HTML string?

How to check if user is logged in to ASP.NET Core web application when using ASP.NET Core Web API to house identity

ASP.Net Core how to get the user role in EF Core and Identity

How to get user Browser name ( user-agent ) in Asp.net Core?

How to make a middleware that can call database to check user claims to authorize a user in asp.net core 2.2

Asp.net Core User Selectable CSS

Asp net Core Get user Windows username

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive