-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathProgram.cs
More file actions
executable file
·113 lines (86 loc) · 3.44 KB
/
Program.cs
File metadata and controls
executable file
·113 lines (86 loc) · 3.44 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.IdentityModel.Tokens.Jwt;
using Client;
using Duende.AccessTokenManagement;
using Duende.AccessTokenManagement.OpenIdConnect;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
var builder = WebApplication.CreateBuilder(args);
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
builder.Services.AddControllersWithViews();
builder.Services.AddHttpClient();
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "oidc";
})
.AddCookie(options =>
{
options.Cookie.Name = "mvc";
options.Events.OnSigningOut = async e =>
{
// automatically revoke refresh token at signout time
await e.HttpContext.RevokeRefreshTokenAsync();
};
})
.AddOpenIdConnect("oidc", options =>
{
options.Authority = Urls.IdentityServer;
// no static client secret
// the secret id created dynamically
options.ClientId = builder.Configuration["ClientId"];
// needed to add JWR / private_key_jwt support
options.EventsType = typeof(OidcEvents);
// NOTE: PushedAuthorization was added in .NET 9 and is turned on by default.
// There is some additional setup involved, which we do nto want to cover in this particular sample.
// See the MvcPar sample if you want to work with PushedAuthorization (PAR).
options.PushedAuthorizationBehavior = PushedAuthorizationBehavior.Disable;
// code flow + PKCE (PKCE is turned on by default)
options.ResponseType = "code";
options.UsePkce = true;
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("scope1");
options.Scope.Add("offline_access");
// not mapped by default
options.ClaimActions.MapJsonKey("website", "website");
// keeps id_token smaller
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
options.MapInboundClaims = false;
options.DisableTelemetry = true;
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "role"
};
});
// add service to create JWTs
builder.Services.AddSingleton<AssertionService>();
// add event handler for OIDC events
builder.Services.AddTransient<OidcEvents>();
// add automatic token management
builder.Services.AddOpenIdConnectAccessTokenManagement();
// add service to create assertions for token management
builder.Services.AddTransient<IClientAssertionService, ClientAssertionService>();
// add HTTP client to call protected API
builder.Services.AddUserAccessTokenHttpClient("client", configureClient: client =>
{
client.BaseAddress = new Uri(Urls.SampleApi);
});
var app = builder.Build();
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapDefaultControllerRoute()
.RequireAuthorization();
app.Run();