Microsoft.AspNetCore.Authentication.JwtBearer
package.If you haven’t created an API in your Auth0 dashboard yet, you can use the interactive selector to create a new
Auth0 API or select an existing API that represents the project you want to integrate with.Alternatively, you can read our getting started guide,
which will help you set up your first API through the Auth0 Dashboard.Note that every API in Auth0 is configured using an API Identifier; your application code will use the API
Identifier as the Audience to validate the access token.messages
resource if users have the manager
access level, and grant write access to that resource if they have the administrator access level.You can define allowed permissions in the Permissions view of the Auth0 Dashboard’s APIs section. The following
example uses the read:messages
scope.Microsoft.AspNetCore.Authentication.JwtBearer
NuGet package:Program.cs
file:AddAuthentication
method. Configure
JwtBearerDefaults.AuthenticationScheme
as the default scheme.
AddJwtBearer
method.
Configure your Auth0 domain as the authority and your Auth0 API Identifier as the audience, and be sure that
your Auth0 domain and API Identifier are set in your application’s appsettings.json file.
sub
claim; in this case, the
User.Identity.Name
will be null
. If you want to map a different claim to
User.Identity.Name
, add it to options.TokenValidationParameters
within the
AddJwtBearer()
call.UseAuthentication
and UseAuthorization
methods under the
var app = builder.Build();
method.
HasScopeRequirement
, which will check whether the
scope
claim issued by your Auth0 tenant is present, and if so, will check that the claim contains
the requested scope.Program.cs
file’s
var builder = WebApplication.CreateBuilder(args);
method, add a call to the
app.AddAuthorization
method.AddPolicy
for each scope.HasScopeHandler
class.[Authorize]
attribute to your controller action (or the entire
controller if you want to protect all of its actions).When securing endpoints that require specific scopes, make sure that the correct scope is present in the
access_token
. To do so, add the Authorize
attribute to the Scoped
action
and pass read:messages
as the policy
parameter.client_id
parameter.client_secret
parameter.audience
parameter./api/private
endpoint:/api/private-scoped
endpoint in a similar way, but ensure that the API permissions are
configured correctly and that the access token includes the read:messages
scope./api/private
and /api/private-scoped
endpoints.Run your application and verify that:GET /api/private
is available for authenticated requests.GET /api/private-scoped
is available for authenticated requests containing an access
token with the read:messages
scope.