c# - IdentityServer4多个WSFederation-providers导致异常

标签 c# asp.net-core .net-core identityserver4 ws-federation

有人告诉我 issue我在这里描述的不是 IdentityServer 中的错误,所以我可能做错了什么:

此代码有效,使用单个 WSFederation 实例作为 QuickStart-project using EFCore 中的身份提供者.

注册提供者:

services.AddAuthentication()
            .AddWsFederation("WsFederation", options =>
            {
                options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                options.Wtrealm = realm;
                options.MetadataAddress = metadata;
                options.Events.OnTicketReceived += OnTicketReceived;
            })

OnTicketReceived 事件处理程序:

/// <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")));

}

一旦添加第二个提供程序,我在尝试使用第二个提供程序时就会收到异常,因为添加的第一个提供程序接受请求并在收到请求后立即抛出异常:

services.AddAuthentication()
.AddWsFederation("WsFederation_LocalHost", "WsFederation_LocalHost", options =>
                {
                    options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                    options.Wtrealm = "urn:aspnetcorerp";
                    options.MetadataAddress = "http://localhost:5000/wsfederation";
                    options.Events.OnTicketReceived += OnTicketReceived;
                    options.RequireHttpsMetadata = false;
                })
                .AddWsFederation("WsFederation_SVN", "WsFederation_SVN", options =>
                {
                    options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                    options.Wtrealm = realm;
                    options.MetadataAddress = metadata;
                    options.Events.OnTicketReceived += OnTicketReceived;
                    options.Events.OnSecurityTokenReceived += OnSecurityTokenReceived;
                })

我得到的异常是这样的 - 如果我通过允许未经请求的登录来修复它,则会发生其他异常,因为它仍然尝试使用错误的提供程序:

System.Exception: Unsolicited logins are not allowed. at Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1.d__12.MoveNext()

我找到了它提出的点 in IdentityServer4 :

public async Task<bool> HandleRequestAsync()
    {
var result = await _inner.HandleRequestAsync();

如果我更改上面的代码以捕获异常,我可以解决此问题,因为它随后会传递正确的提供程序并且登录成功:

var result = false;
try
{
   result = await _inner.HandleRequestAsync();
}
catch (Exception) {}

我对必须 fork IdentityServer4 来解决此问题感到不满意,因此我要求在不更改 IdentityServer 代码的情况下找到解决方案。我可以介入并更改某些内容的地方是我的 WSFederation 端点的配置,对 before starting the ExternalLoginat the callback在 AccountController 中。

AccountController 中的回调:

    [HttpGet]
    public async Task<IActionResult> ExternalLoginCallback()
    {
        // read external identity from the temporary cookie - I don't know how I could change which AuthenticationMiddleware gets called
        var result = await HttpContext.AuthenticateAsync(IdentityServer4.IdentityServerConstants.ExternalCookieAuthenticationScheme);

非常感谢我应该从哪里开始的任何提示。

最佳答案

明白了 - 解决方案是为不同的提供者设置不同的 CallbackPaths:

services.AddAuthentication()
                .AddWsFederation("WsFederation_LocalHost", "WsFederation_LocalHost", options =>
                {
                    options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                    options.Wtrealm = "urn:aspnetcorerp";
                    options.MetadataAddress = "http://localhost:5000/wsfederation";
                    options.Events.OnTicketReceived += OnWsFedTicketReceived;
                    options.RequireHttpsMetadata = false;
                    options.CallbackPath = "/signin-wsfed-localhost";
                })
                .AddWsFederation("WsFederation_SVN", "WsFederation_SVN", options =>
                {
                    options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                    options.Wtrealm = realm;
                    options.MetadataAddress = metadata;
                    options.Events.OnTicketReceived += OnWsFedTicketReceived;
                    options.CallbackPath = "/signin-wsfed-svn";
                })

关于c# - IdentityServer4多个WSFederation-providers导致异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47790174/

相关文章:

c# - 如何隐藏excel列?

c# - 客户端上的抽象类从 swagger 规范 API net core 生成代码

c# - 使用带有 json 补丁的 ASP.NET Core 更新嵌套元素

docker - 在具有两个docker compose的容器之间的docker网络中调用Web API

c# - 在 ASP.NET Core 中为每个 SQL 命令打开/关闭数据库

c# - Docker 中 .NET Core 应用程序的 appSettings.json?

c# - 终止控制台 ReadLine

c# - 使用 C# 将 ASP.NET 代码动态写入 .aspx 文件

c# - .NET MSBuild 目标 AfterTargets=Pack using dotnet build

c# - 具有不同输入参数的类的工厂模式设计