gpt4 book ai didi

asp.net-core - 使用 IdentityServer4 身份验证为 ClaimsIdentity 设置自定义声明

转载 作者:行者123 更新时间:2023-12-02 02:13:02 25 4
gpt4 key购买 nike

我有一个使用 IdentityServer4.TokenValidation 进行身份验证的 ASP.NET Core 2.1 应用程序

authenticationBuilder.AddIdentityServerAuthentication(AuthorizationConstants.IpreoAccountAuthenticationScheme, options =>
{
options.RequireHttpsMetadata = false;
options.ApiName = apiName;
options.ApiSecret = apiSecret;
options.Authority = authority;
options.LegacyAudienceValidation = true;
});

向身份添加自定义声明的最佳方式是什么?考虑到我们仍然需要有机会将 Authorize 属性与 Roles 验证一起使用。

例如,对于承载身份验证,我们可以使用 OnTokenValidated 处理程序,该处理程序在每个请求上触发。但对于 IdentityServerAuthenticationOptions Events 属性是对象类型,并且使用具有 OnTokenValidated 属性的虚拟对象对其进行初始化是行不通的。

我们必须支持 JWT 和引用 token 。我们还需要支持多种身份验证方案

有什么想法或建议吗?

最佳答案

Ruard van Elburg 给了我一个关于使用中间件的好主意。使用此方法进行多个身份验证方案时,我唯一需要更新的是重写 IAuthenticationSchemeProvider 以继续使用 UseAuthentication 中间件。

https://github.com/aspnet/Security/blob/beaa2b443d46ef8adaf5c2a89eb475e1893037c2/src/Microsoft.AspNetCore.Authentication/AuthenticationMiddleware.cs

因此它根据请求内容返回默认方案

我必须做什么:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication();
app.UseMiddleware<ClaimsMiddleware>(); // to set claims for authenticated user
app.UseMvc();
}

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddTransient<IAuthenticationSchemeProvider, CustomAuthenticationSchemeProvider>();
services.AddAuthorization();
services.AddAuthentication // add authentication for multiple schemes
}

关于asp.net-core - 使用 IdentityServer4 身份验证为 ClaimsIdentity 设置自定义声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52170409/

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