WARN BROCKEN

This commit is contained in:
Paul Schneider
2025-02-15 18:40:46 +00:00
parent 4dd7353235
commit 5b5016fedf
5 changed files with 25 additions and 18 deletions

View File

@ -45,7 +45,8 @@ internal class Program
// this defines a CORS policy called "default" // this defines a CORS policy called "default"
options.AddPolicy("default", policy => options.AddPolicy("default", policy =>
{ {
policy.WithOrigins("https://localhost:5003") policy.WithOrigins("https://localhost:5003"
,"http://localhost:5002")
.AllowAnyHeader() .AllowAnyHeader()
.AllowAnyMethod(); .AllowAnyMethod();
}); });
@ -53,7 +54,7 @@ internal class Program
.AddControllersWithViews(); .AddControllersWithViews();
// accepts any access token issued by identity server // accepts any access token issued by identity server
var authenticationBuilder = services.AddAuthentication() var authenticationBuilder = services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options => .AddJwtBearer("Bearer", options =>
{ {
options.IncludeErrorDetails = true; options.IncludeErrorDetails = true;
@ -84,19 +85,18 @@ internal class Program
app app
.UseRouting() .UseRouting()
.UseAuthentication() .UseAuthentication()
.UseAuthorization().UseCors("default") .UseAuthorization().UseCors("default")
.UseEndpoints(endpoints => .UseEndpoints(endpoints =>
{ {
endpoints.MapDefaultControllerRoute() endpoints.MapDefaultControllerRoute().RequireAuthorization("ApiScope");
.RequireAuthorization();
}); });
app.MapIdentityApi<ApplicationUser>().RequireAuthorization("ApiScope"); //app.MapIdentityApi<ApplicationUser>().RequireAuthorization("ApiScope");
app.UseSession(); app.MapGet("/identity", (HttpContext context) =>
new JsonResult(context?.User?.Claims.Select(c => new { c.Type, c.Value }))
).RequireAuthorization("ApiScope");
// app.UseSession();
await app.RunAsync(); await app.RunAsync();
}; };
} }
} }

View File

@ -76,7 +76,9 @@ public static class Config
RedirectUris = { "https://localhost:5003/signin-oidc", RedirectUris = { "https://localhost:5003/signin-oidc",
"http://localhost:5002/signin-oidc" }, "http://localhost:5002/signin-oidc" },
PostLogoutRedirectUris = { "https://localhost:5003/signout-callback-oidc" }, PostLogoutRedirectUris = {
"http://localhost:5002/signout-callback-oidc",
"https://localhost:5003/signout-callback-oidc" },
AllowOfflineAccess = true, AllowOfflineAccess = true,
@ -86,6 +88,10 @@ public static class Config
IdentityServerConstants.StandardScopes.Email, IdentityServerConstants.StandardScopes.Email,
IdentityServerConstants.StandardScopes.OfflineAccess, IdentityServerConstants.StandardScopes.OfflineAccess,
"scope2" } "scope2" }
,
AllowedCorsOrigins=new string[]{"*"},
AllowRememberConsent=true
}, },
}; };

View File

@ -303,14 +303,14 @@ public static class HostingExtensions
string? googleClientSecret = configurationRoot["Authentication:Google:ClientSecret"]; string? googleClientSecret = configurationRoot["Authentication:Google:ClientSecret"];
var authenticationBuilder = services.AddAuthentication() var authenticationBuilder = services.AddAuthentication()
.AddJwtBearer("Bearer", options => .AddJwtBearer("Bearer", options =>
{ {
options.IncludeErrorDetails = true; options.IncludeErrorDetails = true;
options.Authority = "https://localhost:5001"; options.Authority = "https://localhost:5001";
options.TokenValidationParameters = options.TokenValidationParameters =
new() { ValidateAudience = false }; new() { ValidateAudience = false };
}); });
authenticationBuilder.AddGoogle(options => authenticationBuilder.AddGoogle(options =>
{ {
options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme; options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
@ -329,7 +329,9 @@ public static class HostingExtensions
.AddInMemoryClients(Config.Clients) .AddInMemoryClients(Config.Clients)
.AddInMemoryApiScopes(Config.ApiScopes) .AddInMemoryApiScopes(Config.ApiScopes)
.AddAspNetIdentity<ApplicationUser>() .AddAspNetIdentity<ApplicationUser>()
.AddJwtBearerClientAuthentication(); .AddJwtBearerClientAuthentication()
// .AddProfileService<ProfileService>()
;
if (builder.Environment.IsDevelopment()) if (builder.Environment.IsDevelopment())
{ {
identityServerBuilder.AddDeveloperSigningCredential(); identityServerBuilder.AddDeveloperSigningCredential();

View File

@ -130,7 +130,6 @@ namespace cli
{ {
options.ResourcesPath = "Resources"; options.ResourcesPath = "Resources";
}); });
services.Configure<SharedAuthenticationOptions>(options => services.Configure<SharedAuthenticationOptions>(options =>
{ {
options.SignInScheme = "Bearer"; options.SignInScheme = "Bearer";
@ -138,7 +137,7 @@ namespace cli
services.AddTransient<Microsoft.Extensions.WebEncoders.UrlEncoder, UrlEncoder>(); services.AddTransient<Microsoft.Extensions.WebEncoders.UrlEncoder, UrlEncoder>();
services.AddAuthentication(); services.AddAuthentication("Bearer");
services.AddSingleton(typeof(IApplicationEnvironment), svs => PlatformServices.Default.Application); services.AddSingleton(typeof(IApplicationEnvironment), svs => PlatformServices.Default.Application);
services.AddSingleton(typeof(IRuntimeEnvironment), svs => PlatformServices.Default.Runtime); services.AddSingleton(typeof(IRuntimeEnvironment), svs => PlatformServices.Default.Runtime);

View File

@ -44,7 +44,7 @@ namespace testOauthClient.Controllers
var client = new HttpClient(); var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var content = await client.GetStringAsync("https://localhost:6001/api/account/me"); var content = await client.GetStringAsync("https://localhost:6001/identity");
ViewBag.Json = content; ViewBag.Json = content;
return View("json"); return View("json");