如何使用 Sustainsys Saml2 从 Idp 发起的登录中检索声明?

How to Retrieve Claims from Idp-Initiated Login Using Sustainsys Saml2?

我正在尝试向具有 ASP.NET Core Identity(不是 IdentityServer)的 ASP.NET Core MVC 应用程序添加对 SAML 身份验证的支持。使用 StubIdp 进行测试时的流程 "works" - SAMLResponse 被发布到 /Saml2/Acs,我被重定向到带有 Identity.External cookie 的应用程序,但我的 ClaimsPrincipal 是空的且未经身份验证.即使我使用数据库中已存在的用户的 NameID,声明也完全是空的。

我还在控制台日志中看到以下内容:
Sustainsys.Saml2.AspNetCore2.Saml2Handler: Information: Successfully processed SAML response Microsoft.IdentityModel.Tokens.Saml2.Saml2Id and authenticated JohnDoe

我安装了Sustainsys.Saml2.AspNetCore2包,并在startup.cs中添加服务配置如下:

services.AddAuthentication()
                .AddSaml2(async options =>
                {
                    var azureServiceTokenProvider = new AzureServiceTokenProvider();
                    var keyVaultClient = new KeyVaultClient(
                        new KeyVaultClient.AuthenticationCallback(
                            azureServiceTokenProvider.KeyVaultTokenCallback));

                    var certificateSecret = await keyVaultClient.GetSecretAsync($"https://{Configuration["KeyVaultName"]}.vault.azure.net/", Configuration["ServiceProviderCertName"]);
                    var privateKeyBytes = Convert.FromBase64String(certificateSecret.Value);

                    options.SPOptions.EntityId = new EntityId(Configuration["BaseUrl"] + "/Saml2");
                    options.SPOptions.ReturnUrl = new Uri(Configuration["BaseUrl"]);

                    IdentityProvider idp = new IdentityProvider(
                            new EntityId("https://stubidp.sustainsys.com/Metadata"), options.SPOptions)
                    {
                        LoadMetadata = true,
                        MetadataLocation = "https://stubidp.sustainsys.com/Metadata",
                        AllowUnsolicitedAuthnResponse = true
                    };

                    options.IdentityProviders.Add(idp);

                    options.SPOptions.ServiceCertificates.Add(new X509Certificate2(privateKeyBytes));
                });

Configuration["BaseUrl"] 是我的应用程序的基础 URL,在本例中是本地主机端口。

我显然遗漏了什么,但我不知道是什么。我是否需要以某种方式明确 connect/map Saml2 服务到 ASP.NET Core Identity?

能够根据 this GitHub issue 中的评论解决此问题。

我的评论解释了我是如何实施解决方法的:https://github.com/Sustainsys/Saml2/issues/1030#issuecomment-616842796