gpt4 book ai didi

asp.net-core - ASP.NET Core 和 OpenID 连接重定向到外部身份提供程序

转载 作者:行者123 更新时间:2023-12-03 22:19:06 25 4
gpt4 key购买 nike

我使用 OpenID Connect 构建了一个身份提供程序,以利用 OAuth2 访问 token 提供身份验证和授权。服务器上的授权工作流程有效;但是,当身份验证失败时,我似乎无法让我的 ASP.NET Core 客户端自动重定向到 OpenID Connect 提供程序。我目前只收到401。

这是我的startup.cs:

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}

app.UseStaticFiles();

app.UseCookieAuthentication();

var options = new OpenIdConnectOptions
{
Authority = "http://localhost:63467",
AutomaticAuthenticate = true,
AutomaticChallenge = true,
AuthenticationMethod = OpenIdConnectRedirectBehavior.RedirectGet,
AuthenticationScheme = "oidc",
ClientId = "2",
ClientSecret = "alskghalsd",
Configuration =
new OpenIdConnectConfiguration
{
AuthorizationEndpoint =
"http://localhost:63467/connect/authorize",
TokenEndpoint =
"http://localhost:63467/connect/token"
},
PostLogoutRedirectUri = "/",
ResponseType = "Code",
RemoteSignOutPath = "/signout",
UseTokenLifetime = true,
SaveTokens = true,
SignInScheme = "Cookies",
RequireHttpsMetadata = false
};
options.Scope.AddRange(new[] { "openid name role profile" });
app.UseOpenIdConnectAuthentication(options);

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}

最佳答案

我最终通过使用 IdentityServer 将应用程序作为身份端点托管并相应地配置了我的客户端来解决这个问题。

        public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc(
opts =>
{
// custom api exception filter
opts.Filters.Add(typeof(ApiExceptionFilter));
});
services.Configure<IISOptions>(
options =>
{
options.AutomaticAuthentication = false;
options.ForwardClientCertificate = false;
options.ForwardWindowsAuthentication = false;
});

services.AddSingleton(this.Configuration);
services.AddIdentityServer();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme = "Cookies" });
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

// get values from config
IConfigurationSection clientSettings = this.Configuration.GetSection("AppSettings:ClientSettings");
string[] scopes = clientSettings["Scope"].Split(' ');
var oidcOptions = new OpenIdConnectOptions
{
AuthenticationScheme = clientSettings["AuthenticationScheme"],
SignInScheme = clientSettings["SignInScheme"],
Authority = clientSettings["Authority"],
ClaimsIssuer = clientSettings["ClaimsIssuer"],
RequireHttpsMetadata =
clientSettings.GetValue<bool>("RequireHttpsMetadata"),
ClientId = clientSettings["ClientId"],
ClientSecret = clientSettings["ClientSecret"],
ResponseType = clientSettings["ResponseType"],
GetClaimsFromUserInfoEndpoint = clientSettings.GetValue<bool>("GetClaimsFromUserInfoEndpoint"),
SaveTokens = clientSettings.GetValue<bool>("SaveTokens")
};
foreach (var scope in scopes)
{
oidcOptions.Scope.Add(scope);
}

app.UseOpenIdConnectAuthentication(oidcOptions);

app.UseStaticFiles();

app.UseMvcWithDefaultRoute();
}

在将此标记为答案以允许其他人提供替代解决方案之前,我将等待一段时间。

关于asp.net-core - ASP.NET Core 和 OpenID 连接重定向到外部身份提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41945703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com