CarterCommunity/Carter

How to Authorize Endpoint for specific Role

xts-velkumars opened this issue · 1 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();
}