Skip to content

Instantly share code, notes, and snippets.

@neiesc
Created May 3, 2024 12:22
Show Gist options
  • Save neiesc/12d7f38b3b8a73012f56397d6a29902c to your computer and use it in GitHub Desktop.
Save neiesc/12d7f38b3b8a73012f56397d6a29902c to your computer and use it in GitHub Desktop.
Backend AzureAD
using Microsoft.EntityFrameworkCore;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.RateLimiting;
using System.Threading.RateLimiting;
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.Authentication.JwtBearer;
namespace Infrastructure;
public static class ConfigurationApp
{
public static void ConfigureServices(WebApplicationBuilder builder)
{
var tenantId = $"{builder.Configuration["AzureAD:TenantId"]}";
var authority = $"{builder.Configuration["AzureAD:Instance"]}/{tenantId}";
var audience = new List<string>
{
$"{builder.Configuration["AzureAD:ClientId"]}",
$"{builder.Configuration["AzureAD:Scope"]}"
};
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = authority;
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
ValidAudiences = audience,
ValidIssuers = new List<string>
{
$"https://sts.windows.net/{tenantId}",
$"https://sts.windows.net/{tenantId}/v2.0",
}
};
});
builder.Services.AddAuthorization();
...
}
public static void ConfigureApp(WebApplication app)
{
...
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment