-
-
Save code-atom/2bebe459c756f15a519c3136b5be1a89 to your computer and use it in GitHub Desktop.
Shared API Key Authentication Handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Authorization; | |
var builder = WebApplication.CreateBuilder(args); | |
// Add services to the container. | |
builder.Services.AddAuthentication() | |
.AddSharedApiKey(option => option.SharedApiKey = "Test"); | |
builder.Services.AddAuthorization(); | |
var app = builder.Build(); | |
// Configure the HTTP request pipeline. | |
app.UseHttpsRedirection(); | |
app.UseAuthentication(); | |
app.UseAuthorization(); | |
var summaries = new[] | |
{ | |
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | |
}; | |
app.MapGet("/weatherforecast", () => | |
{ | |
var forecast = Enumerable.Range(1, 5).Select(index => | |
new WeatherForecast | |
( | |
DateOnly.FromDateTime(DateTime.Now.AddDays(index)), | |
Random.Shared.Next(-20, 55), | |
summaries[Random.Shared.Next(summaries.Length)] | |
)) | |
.ToArray(); | |
return forecast; | |
}).RequireAuthorization(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()); | |
app.Run(); | |
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) | |
{ | |
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment