我正在使用Identity和Sustainsys.Saml2(用于SAML身份验证)开发ASP.NET Core 2应用程序。我已经在Startup.cs文件中进行了必要的配置。现在,当我运行项目并尝试使用SAML2登录(作为外部登录名)时,输入凭据后出现以下错误:
SecurityTokenInvalidAudienceException:IDX10214:受众验证 失败了观众:“ [PII隐藏]”。不匹配: validationParameters.ValidAudience:“ [PII隐藏]”或 validationParameters.ValidAudiences:“ [PII隐藏]”。 Microsoft.IdentityModel.Tokens.Validators.ValidateAudience(IEnumerable 受众,SecurityToken securityToken,TokenValidationParameters validateParameters) Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.ValidateConditions(Saml2SecurityToken samlToken,TokenValidationParameters validateParameters) Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.ValidateToken(字符串 令牌,TokenValidationParametersvalidationParameters,输出 SecurityToken validatedToken) Sustainsys.Saml2.Saml2P.Saml2Response + d__60.MoveNext() System.Collections.Generic.List..ctor(IEnumerable集合) System.Linq.Enumerable.ToList(IEnumerable源) Sustainsys.Saml2.Saml2P.Saml2Response.GetClaims(IOptions选项, IDictionary relayData) Sustainsys.Saml2.WebSso.AcsCommand.ProcessResponse(IOptions选项, Saml2Response samlResponse,StoredRequestStatestoredRequestState) Sustainsys.Saml2.WebSso.AcsCommand.Run(HttpRequestData请求, IOptions选项) Sustainsys.Saml2.AspNetCore2.Saml2Handler + d__12.MoveNext() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Authentication.AuthenticationMiddleware + d__6.MoveNext() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware + d__4.MoveNext() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware + d__6.MoveNext() System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware + d__6.MoveNext() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware + d__7.MoveNext()
我不明白这是什么意思。我想念什么吗?
这是启动文件中的内容
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,
});
});
预先感谢...
答案 0 :(得分:4)
据我所知,此错误清楚地表明,SAML令牌中的受众与启动配置中的值不同。比较这些值可能会有所帮助。有时由于区分大小写的比较导致验证失败,因此,在这种情况下,您的听众必须在令牌和配置中进行。
根据源代码(Saml2Response)并如Anders Abel所指出,ValidAudience
属性是从您在此处配置的SPOptions.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",
};
因此,您应该将已配置的EntityId
值与saml令牌中的值进行比较,该值看起来像这样:
<saml:Audience>The value here should be the same as in your startup configuration</saml:Audience>
答案 1 :(得分:0)
IDX10214
:如果您使用的是 Microsoft.Identity.Web
版本 1.4.1 或类似版本,请检查 this 部分并且您收到此异常(字面上复制,并且您必须更改 appsettings.json
中的日志级别{1}} 以查看此内容):
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[1]
Failed to validate the token.
Microsoft.IdentityModel.Tokens.SecurityTokenInvalidAudienceException: IDX10214: Audience validation failed. Audiences: 'System.String'. Did not match: validationParameters.ValidAudience: 'System.String' or validationParameters.ValidAudiences: 'System.String'.
at Microsoft.IdentityModel.Tokens.Validators.ValidateAudience(IEnumerable`1 audiences, SecurityToken securityToken, TokenValidationParameters validationParameters)
at Microsoft.Identity.Web.Resource.RegisterValidAudience.ValidateAudience(IEnumerable`1 audiences, SecurityToken securityToken, TokenValidationParameters validationParameters)
at Microsoft.IdentityModel.Tokens.Validators.ValidateAudience(IEnumerable`1 audiences, SecurityToken securityToken, TokenValidationParameters validationParameters)
at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateAudience(IEnumerable`1 audiences, JwtSecurityToken jwtToken, TokenValidationParameters validationParameters)
at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateTokenPayload(JwtSecurityToken jwtToken, TokenValidationParameters validationParameters)
at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken(String token, TokenValidationParameters validationParameters, SecurityToken& validatedToken)
at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
实际看到那些 System.String
的价值会更有帮助。如果您使用 GUID 或易于记忆和可读的东西都无关紧要。
基本上,您必须将 Audience
属性添加到 appsettings.json
并且 that 必须等于 Azure 门户中的 Application ID URI
。我没有运气破解 ClientId
并且匹配 Application ID URI
。这不是最终的解决方案 - AFAIK 仍然必须等于 Azure 门户中的 Application (client) ID
,即没有任何前缀或后缀的 GUID。