Sign in

Protect API Routes

This guide shows how to protect .NET API endpoints using the MonoCloud API Authentication .NET SDK.

Because the SDK registers a standard ASP.NET Core authentication scheme, you protect endpoints with the built-in [Authorize] attribute, RequireAuthorization(), and the authorization policy system.

What you'll cover

  • Protect all endpoints globally
  • Protect individual endpoints
  • Require specific groups

Before you begin

This guide assumes you've completed the .NET quickstart or the installation guide.

You should already have:

  • An ASP.NET Core project
  • The MonoCloud.Authentication.Api SDK installed
  • The MonoCloud section configured in appsettings.json

Protect all endpoints globally

Add a fallback authorization policy to require a valid access token for every endpoint.

Program.cs
using System.Security.Claims;
using MonoCloud.Authentication.Api;

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddAuthentication(MonoCloudAuthenticationDefaults.AuthenticationScheme)
    .AddMonoCloudAuthentication(options =>
    {
        options.TenantDomain = builder.Configuration["MonoCloud:TenantDomain"];
        options.Audience = builder.Configuration["MonoCloud:Audience"];
    });

builder.Services.AddAuthorization(options =>
{
    options.FallbackPolicy = options.DefaultPolicy;
});

var app = builder.Build();

app.UseAuthentication();
app.UseAuthorization();

app.MapGet("/api/data", (ClaimsPrincipal user) => new
{
    claims = user.Claims.Select(claim => new { claim.Type, claim.Value }),
});

app.Run();

How it works:

  • AddMonoCloudAuthentication() validates the Authorization: Bearer <token> header
  • The FallbackPolicy applies to every endpoint that doesn't specify its own authorization
  • Validated claims are available on HttpContext.User

Protect individual endpoints

Apply authorization to specific endpoints instead of globally. Mark public endpoints with .AllowAnonymous() — they stay public even when a FallbackPolicy is set.

Program.cs
app.MapGet("/api/public", () => new { message = "Public data" })
   .AllowAnonymous();

app.MapGet("/api/protected", (ClaimsPrincipal user) => new
{
    claims = user.Claims.Select(claim => new { claim.Type, claim.Value }),
});

Require specific groups

Set RoleClaimType so the SDK maps the token's group memberships to roles, then require a role.

Program.cs
builder.Services
    .AddAuthentication(MonoCloudAuthenticationDefaults.AuthenticationScheme)
    .AddMonoCloudAuthentication(options =>
    {
        options.TenantDomain = builder.Configuration["MonoCloud:TenantDomain"];
        options.Audience = builder.Configuration["MonoCloud:Audience"];
        options.RoleClaimType = "groups";
    });

builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("engineering", policy => policy.RequireRole("engineering"));
});
Program.cs
app.MapGet("/api/team", () => "Team data")
   .RequireAuthorization("engineering");

Behavior:

  • Returns 401 Unauthorized if the token is missing or invalid
  • Returns 403 Forbidden if the token is valid but the user is not in the required group
  • Executes the handler if the user belongs to the required group

Response behavior

ScenarioStatus codeResponse
Missing or invalid token401Unauthorized
Valid token, missing group403Forbidden
Valid token, authorizedEndpoint handler executes
© 2024 MonoCloud. All rights reserved.