-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
27 lines (24 loc) · 1.11 KB
/
Program.cs
File metadata and controls
27 lines (24 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Identity;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddAuthentication(defaultScheme: OpenIdConnectDefaults.AuthenticationScheme)
.AddOpenIdConnect(openIdConnectOptions =>
{
openIdConnectOptions.SignInScheme = IdentityConstants.ExternalScheme;
openIdConnectOptions.ResponseType = OpenIdConnectResponseType.Code;
openIdConnectOptions.ClientId = "https://localhost";
openIdConnectOptions.MetadataAddress = "https://api.passwordless.id/.well-known/openid-configuration";
})
.AddExternalCookie();
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapGet("/", () => "Go to /private to authenticate");
app.MapGet("/private", context => {
string username = context.User.FindFirst("preferred_username")?.Value ?? string.Empty;
return context.Response.WriteAsync($"Hello, {username}!");
}).RequireAuthorization();
app.Run();