ASP.NET Core: OpenID Connect-voorbeeld met PKCE
Inloggen met Dazr Identity: gratis OpenID Connect, gehost in de EU, met passkeys, e-mailcodes en Google- of Microsoft-accounts achter één knop.
Je hebt drie waarden uit de ontwikkelaarsconsole nodig: de client-ID, het clientgeheim (alleen voor webserver-apps) en de redirect-URI die je hebt geregistreerd. De rest staat in het discovery-document op https://identity.dazr.eu/.well-known/openid-configuration.
ASP.NET Core
Gebruik de ingebouwde OpenID Connect-handler (Microsoft.AspNetCore.Authentication.OpenIdConnect, .NET 8 of nieuwer). Registreer een webserver-app met de redirect-URI https://app.example.eu/signin-oidc en, voor uitloggen, https://app.example.eu/signout-callback-oidc. Drie instellingen zijn belangrijk: Dazr geeft de code terug in de querystring, dus zet ResponseMode op query (de handler gebruikt standaard form_post); PKCE is verplicht; en ID-tokens zijn ondertekend met ES256.
// Program.cs
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.Authority = "https://identity.dazr.eu";
options.ClientId = builder.Configuration["Dazr:ClientId"];
options.ClientSecret = builder.Configuration["Dazr:ClientSecret"];
options.ResponseType = OpenIdConnectResponseType.Code;
options.ResponseMode = OpenIdConnectResponseMode.Query;
options.UsePkce = true;
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true; // keeps the ID token for sign-out (id_token_hint)
options.MapInboundClaims = false; // keep sub, email and name as they are
options.TokenValidationParameters.NameClaimType = "name";
options.TokenValidationParameters.ValidAlgorithms = new[] { "ES256" };
});
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapGet("/signin", () => Results.Challenge(new() { RedirectUri = "/" }));
app.MapGet("/signout", () => Results.SignOut(new() { RedirectUri = "/" },
new[] { CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme }));
app.Run();
De Dazr-sub is User.FindFirst("sub")?.Value. Sla die op als accountsleutel. Bewaar het client secret in user secrets of een vault, nooit in appsettings.json.
sub is pairwise: hetzelfde voor elke app van je organisatie, anders voor elke andere organisatie. Hij verandert nooit voor een persoon, een e-mailadres wel. Sla sub op als sleutel van het account en behandel email als contactgegeven.
Meer voor ontwikkelaars
OntwikkelaarsdocumentatieAlle integratiehandleidingenElke OpenID Connect-bibliotheekPrijzen