mirror of
https://github.com/fiodarsazanavets/aspire-13-examples.git
synced 2026-06-20 12:23:14 +00:00
56 lines
1.8 KiB
C#
56 lines
1.8 KiB
C#
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
|
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
|
|
|
|
namespace OnlineShop.Web;
|
|
|
|
public static class AuthExtensions
|
|
{
|
|
public static void ConfigureWebAppOpenIdConnect(this AuthenticationBuilder authentication)
|
|
{
|
|
// Named options
|
|
authentication.Services
|
|
.AddOptions<OpenIdConnectOptions>(
|
|
OpenIdConnectDefaults.AuthenticationScheme)
|
|
.Configure<
|
|
IConfiguration,
|
|
IHttpClientFactory,
|
|
IHostEnvironment>(Configure);
|
|
|
|
// Unnamed options
|
|
authentication.Services.AddOptions<OpenIdConnectOptions>()
|
|
.Configure<
|
|
IConfiguration,
|
|
IHttpClientFactory,
|
|
IHostEnvironment>(Configure);
|
|
|
|
static void Configure(
|
|
OpenIdConnectOptions options,
|
|
IConfiguration configuration,
|
|
IHttpClientFactory httpClientFactory,
|
|
IHostEnvironment hostEnvironment)
|
|
{
|
|
var backchannelHttpClient =
|
|
httpClientFactory.CreateClient(
|
|
"OidcBackchannel");
|
|
|
|
options.Backchannel = backchannelHttpClient;
|
|
options.Authority =
|
|
backchannelHttpClient
|
|
.GetIdpAuthorityUri().ToString();
|
|
options.ClientId = "webapp";
|
|
options.ClientSecret =
|
|
Environment
|
|
.GetEnvironmentVariable(
|
|
"Identity__ClientSecret");
|
|
options.ResponseType =
|
|
OpenIdConnectResponseType.Code;
|
|
options.SaveTokens = true;
|
|
options.RequireHttpsMetadata =
|
|
!hostEnvironment.IsDevelopment();
|
|
options.MapInboundClaims = false;
|
|
}
|
|
}
|
|
|
|
}
|