SecurityTokenInvalidAudienceException:IDX10214:听众验证失败

Onl*_*eEA 1 c# saml asp.net-core sustainsys-saml2

我正在使用Identity和Sustainsys.Saml2(用于SAML身份验证)开发ASP.NET Core 2应用程序。我已经在Startup.cs文件中进行了必要的配置。现在,当我运行项目并尝试使用SAML2登录(作为外部登录名)时,输入凭据后出现以下错误:

SecurityTokenInvalidAudienceException:IDX10214:听众验证失败。观众:“ [PII隐藏]”。不匹配:validationParameters.ValidAudience:“ [PII隐藏]”或validationParameters.ValidAudiences:“ [PII隐藏]”。Microsoft.IdentityModel。令牌令牌,TokenValidationParameters验证参数,不包括SecurityToken validatedToken)Sustainsys.Saml2.Saml2P.Saml2Response + d__60.MoveNext()System.Collections.Generic。

我不明白这是什么意思。我想念什么吗?

这是我在启动文件中的内容

services.AddAuthentication()
        .AddSaml2(options => 
        {
            var spOptions = new SPOptions
            {
                EntityId = new EntityId("https://localhost:44373/Saml2"),
                ReturnUrl = new Uri("https://localhost:44373"),
                MinIncomingSigningAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1",                  
            };                

            options.SPOptions = spOptions;
            options.IdentityProviders.Add(new IdentityProvider(new EntityId("https://www.example.com/SSO/SAML/App"), options.SPOptions)
            {
                AllowUnsolicitedAuthnResponse = false,                  
                MetadataLocation = "https://www.example.com/SSO/SAMLMetadata/App",                  
                LoadMetadata = true,                  
            }); 
        });
Run Code Online (Sandbox Code Playgroud)

提前致谢...

Nam*_*aJV 7

据我所知,此错误清楚地表明,SAML令牌中的受众与启动配置中的值不同。比较这些值可能会有所帮助。有时由于区分大小写的比较而导致验证失败,因此,在这种情况下,您的听众是在令牌和配置中进行的,因此应注意。

根据源代码(Saml2Response)并如Anders Abel所指出的,ValidAudienceSPOptions.EntityId此处配置的属性初始化属性:

var spOptions = new SPOptions
{
    EntityId = new EntityId("https://localhost:44373/Saml2"),
    ReturnUrl = new Uri("https://localhost:44373"),
    MinIncomingSigningAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1",                  
};
Run Code Online (Sandbox Code Playgroud)

因此,您应该将EntityId自己配置的值与saml令牌中的值进行比较,如下所示:

<saml:Audience>The value here should be the same as in your startup configuration</saml:Audience>
Run Code Online (Sandbox Code Playgroud)

  • @AndersAbel,您好,我通过插入以下代码行“ IdentityModelEventSource.ShowPII = true;”来使PII显示在错误消息中,并且错误现在显示此“受众群体:” https:// localhost:44373 / Saml2 / Acs'。不匹配:validationParameters.ValidAudience:“ https:// localhost:44373 / Saml2”或validationParameters.ValidAudiences:“ null” (3认同)
  • 嗨@OnlyOneEA 感谢您提供代码片段`IdentityModelEventSource.ShowPII = true;`,它可以添加到正在添加到 Startup.cs 程序的服务中。我最初对它会去哪里感到困惑,但后来想通了。终于想通了,即使我有一个错过比赛的问题。 (2认同)