2025-02-16 17:28:38 +00:00
|
|
|
/*
|
|
|
|
|
Copyright (c) 2024 HigginsSoft, Alexander Higgins - https://github.com/alexhiggins732/
|
2024-11-12 08:32:15 +00:00
|
|
|
|
2025-02-16 17:28:38 +00:00
|
|
|
Copyright (c) 2018, Brock Allen & Dominick Baier. All rights reserved.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
|
|
|
|
Source code and license this software can be found
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
2025-02-17 23:56:28 +00:00
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2025-02-16 17:28:38 +00:00
|
|
|
|
2025-07-10 15:19:28 +01:00
|
|
|
JwtSecurityTokenHandler.DefaultMapInboundClaims = true;
|
2025-07-10 09:16:58 +01:00
|
|
|
|
2025-02-16 17:28:38 +00:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
2026-03-01 20:14:54 +00:00
|
|
|
var authSection = builder.Configuration.GetSection("Authentication");
|
|
|
|
|
|
|
|
|
|
var issuer = authSection.GetValue<String>("Issuer");
|
|
|
|
|
var clientSection = authSection.GetSection("Client");
|
|
|
|
|
var clientId = clientSection.GetValue<String>("Id");
|
|
|
|
|
var clientSecret = clientSection.GetValue<String>("Secret");
|
|
|
|
|
|
|
|
|
|
|
2025-02-16 17:28:38 +00:00
|
|
|
builder.Services.AddControllersWithViews();
|
2026-03-01 20:14:54 +00:00
|
|
|
|
2025-02-16 17:28:38 +00:00
|
|
|
builder.Services
|
|
|
|
|
.AddAuthentication(options =>
|
2024-11-12 08:32:15 +00:00
|
|
|
{
|
2026-03-01 20:14:54 +00:00
|
|
|
options.DefaultScheme = Constants.INTERNAL_SCHEME;
|
|
|
|
|
options.DefaultChallengeScheme = Constants.EXTERNAL_SCHEME;
|
2025-02-16 17:28:38 +00:00
|
|
|
})
|
2026-03-01 20:14:54 +00:00
|
|
|
.AddCookie(Constants.INTERNAL_SCHEME)
|
|
|
|
|
.AddOpenIdConnect(Constants.EXTERNAL_SCHEME, options =>
|
2025-02-16 17:28:38 +00:00
|
|
|
{
|
2026-03-01 20:14:54 +00:00
|
|
|
options.Authority = issuer;
|
|
|
|
|
options.ClientId = clientId;
|
|
|
|
|
options.ClientSecret = clientSecret;
|
|
|
|
|
options.ResponseType = Constants.AUTHENTICATION_RESPONSE_TYPE;
|
2025-02-16 17:28:38 +00:00
|
|
|
|
2025-02-16 22:40:51 +00:00
|
|
|
options.Scope.Add("openid");
|
|
|
|
|
options.Scope.Add("profile");
|
2025-02-16 17:28:38 +00:00
|
|
|
options.Scope.Add("scope2");
|
2025-02-17 23:56:28 +00:00
|
|
|
options.MapInboundClaims = true;
|
2025-07-10 15:19:28 +01:00
|
|
|
options.ClaimActions.MapUniqueJsonKey("preferred_username", "preferred_username");
|
2025-02-17 23:56:28 +00:00
|
|
|
options.ClaimActions.MapUniqueJsonKey("gender", "gender");
|
2025-02-16 17:28:38 +00:00
|
|
|
options.SaveTokens = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
using (var app = builder.Build())
|
|
|
|
|
{
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
else
|
|
|
|
|
app.UseExceptionHandler("/Home/Error");
|
|
|
|
|
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
app.MapDefaultControllerRoute().RequireAuthorization();
|
|
|
|
|
|
|
|
|
|
await app.RunAsync();
|
|
|
|
|
}
|