Com*_*eak 0 c# ws-federation .net-core asp.net-core identityserver4
我正在尝试向 IdentityServer4 添加 WSFederation 身份验证提供程序。最近Microsoft.AspNetCore.Authentication.WsFederation Preview 2发布了,我能够将它添加到普通的 Asp.NetCore 应用程序中。
但是我很难将它添加到IdentityServer4 EntityFramework-Quickstart-Example 中。
这就是我在 Startup.cs/ConfigureServices-method 中添加 AuthenticationProvider 的方式:
services.AddAuthentication()
.AddWsFederation("WsFederation", options =>
{
options.Wtrealm = realm;
options.MetadataAddress = metadata;
})
Run Code Online (Sandbox Code Playgroud)
我在前端获得了 WSFederation 按钮,我也可以登录。但是在回调之后我收到这个错误:
InvalidOperationException:子声明缺少 IdentityServer4.Hosting.IdentityServerAuthenticationService.AssertRequiredClaims(ClaimsPrincipal principal)
我可以理解这是从哪里来的,它是IdentityServer4 的 IdentityServerAuthenticationService.cs 中的这一行,它需要一个“子”声明——WSFed 不会返回这样的声明:
if (principal.FindFirst(JwtClaimTypes.Subject) == null) throw new InvalidOperationException("sub claim is missing");
Run Code Online (Sandbox Code Playgroud)
据我所知,我无法配置此声明,尽管快速入门项目中有一些表似乎可以用于此目的,尤其是那些 2:

我已经尝试将我想使用的声明而不是主题声明添加到表中,但它似乎没有任何影响,我也不知道数据库如何映射到我在代码中添加的身份提供者. 我很乐意为您提供有关后续步骤的任何提示,或者如果您知道任何示例,那就更好了。
PS:已经有一些关于这个主题的问题,但它们都是在 .Net Core 实现 WSFederation 之前可用的,或者参考 WSFederation 服务器的示例,而不是客户端。
感谢@leastprivilege,我实施了以下解决方法来提供子声明:
services.AddAuthentication()
.AddWsFederation("WsFederation", options =>
{
options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
options.Wtrealm = realm;
options.MetadataAddress = metadata;
options.Events.OnTicketReceived += OnTicketReceived;
})
/* some more code inbetween */
/// <summary>
/// Transform the UPN-claim to the sub-claim to be compatible with IdentityServer4
/// </summary>
private async Task OnTicketReceived(TicketReceivedContext ticketReceivedContext)
{
var identity = ticketReceivedContext.Principal.Identities.First();
identity.AddClaim(new Claim("sub", ticketReceivedContext.Principal.FindFirstValue("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2262 次 |
| 最近记录: |