yavsc/src/sampleWebAsWebApiClient/Startup.cs

75 lines
2.5 KiB
C#
Raw Normal View History

2025-02-09 12:04:14 +00:00
using Microsoft.AspNetCore.Authentication;
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
2019-01-01 20:14:34 +00:00
using Microsoft.Extensions.DependencyInjection;
2024-11-12 08:32:15 +00:00
using Microsoft.Extensions.Hosting;
2025-02-09 12:04:14 +00:00
using Microsoft.IdentityModel.Tokens;
2024-11-12 08:32:15 +00:00
using System.IdentityModel.Tokens.Jwt;
2019-01-01 20:14:34 +00:00
2025-02-09 16:57:10 +00:00
public class Startup
{
public void ConfigureServices(IServiceCollection services)
2019-01-01 20:14:34 +00:00
{
2025-02-09 16:57:10 +00:00
services.AddControllersWithViews();
2019-01-01 20:14:34 +00:00
2025-02-09 16:57:10 +00:00
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
2019-01-01 20:14:34 +00:00
2025-02-09 16:57:10 +00:00
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "Yavsc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("Yavsc", options =>
{
options.Authority = "https://localhost:5001";
options.ClientId = "mvc";
options.ClientSecret = "49C1A7E1-0C79-4A89-A3D6-A37998FB86B0";
options.ResponseType = "code";
options.UsePkce = true;
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
2025-02-11 04:45:05 +00:00
options.Scope.Add("offline_access");
options.Scope.Add("scope2");
2025-02-09 16:57:10 +00:00
options.GetClaimsFromUserInfoEndpoint = true;
2025-02-09 12:04:14 +00:00
options.SaveTokens = true;
2025-02-09 16:57:10 +00:00
options.ClaimActions.MapUniqueJsonKey("http://schemas.microsoft.com/ws/2008/06/identity/claims/role", "http://schemas.microsoft.com/ws/2008/06/identity/claims/role");
2025-02-09 12:04:14 +00:00
2025-02-09 16:57:10 +00:00
options.ClaimActions.MapUniqueJsonKey("role", "http://schemas.microsoft.com/ws/2008/06/identity/claims/role");
options.ClaimActions.MapUniqueJsonKey("roles", "http://schemas.microsoft.com/ws/2008/06/identity/claims/role");
2025-02-09 12:04:14 +00:00
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
2025-02-09 16:57:10 +00:00
RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
2025-02-09 12:04:14 +00:00
};
2025-02-11 04:45:05 +00:00
2025-02-09 16:57:10 +00:00
});
}
2019-01-01 20:14:34 +00:00
2025-02-09 16:57:10 +00:00
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
2019-01-01 20:14:34 +00:00
{
2025-02-09 16:57:10 +00:00
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
2019-01-01 20:14:34 +00:00
2025-02-09 16:57:10 +00:00
app.UseStaticFiles();
2019-01-01 20:14:34 +00:00
2025-02-09 16:57:10 +00:00
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
2019-01-01 20:14:34 +00:00
2025-02-09 16:57:10 +00:00
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute()
.RequireAuthorization();
});
2019-01-01 20:14:34 +00:00
}
2025-02-09 16:57:10 +00:00
}