CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to Authorize Endpoint for specific Role

xts-velkumars opened this issue · comments

@jchannon, Is there any example how to use "RequiresClaims"

I couldn't find any Extension Method for "RequiresClaims" Carter V7.0.0

Basically, I want to achieve below

[Route("/api/users")]
[AuthorizeRole(RoleType.SuperAdmin)]
public async Task<IEnumerable<UsersViewModel>> GetUsers(CancellationToken cancellationToken)
{
       return await mediator.Send(new UsersQuery(), cancellationToken);
}

Use RequireAuthorization on your route definition or on the module in it's constructor and pass in your roles/policies:

 app.MapGet("/", () =>
        {
          
            return "Hi";
        }).RequireAuthorization();
public DirectorsModule() : base("/directors")
    {
         this.RequireAuthorization();
}