azure-activedirectory-identitymodel-extensions-for-dotnet: [Bug] SamlSecurityTokenHandler and Saml2SecurityTokenHandler does not fetch configuration from ConfigurationManager on ValidateTokenAsync
Which version of Microsoft.IdentityModel are you using?
Microsoft.AspNetCore.Authentication.WsFederation 8.0.0 Microsoft.IdentityModel.Protocols.WsFederation 7.0.3 Microsoft.IdentityModel.Tokens.Saml 7.0.3
Where is the issue?
- M.IM.JsonWebTokens
- M.IM.KeyVaultExtensions
- M.IM.Logging
- M.IM.ManagedKeyVaultSecurityKey
- M.IM.Protocols
- M.IM.Protocols.OpenIdConnect
- M.IM.Protocols.SignedHttpRequest
- M.IM.Protocols.WsFederation
- M.IM.TestExtensions
- M.IM.Tokens
- M.IM.Tokens.Saml
- M.IM.Validators
- M.IM.Xml
- S.IM.Tokens.Jwt
- Other (please describe)
- M.AspNetCore.Authentication.WsFederation
Is this a new or an existing app? The app is in production and I have upgraded to a new version of Microsoft.AspNetCore.Authentication.WsFederation and Microsoft.IdentityModel.Tokens.Saml in development.
Repro
var wsfedmessage = new WsFederationMessage();
wsfedmessage.Wa = "wsignin1.0";
wsfedmessage.Wresult = @"<your wresult here>";
wsfedmessage.Wctx =
"your wctx here";
var token = wsfedmessage.GetToken();
var handler = new SamlSecurityTokenHandler();
var validationParameters = new TokenValidationParameters();
validationParameters.ConfigurationManager = new ConfigurationManager<WsFederationConfiguration>(
"https://<your wsfederation host here>/FederationMetadata/2007-06/FederationMetadata.xml",
new WsFederationConfigurationRetriever());
var result = await handler.ValidateTokenAsync(token, validationParameters);
if (!result.IsValid)
{
throw result.Exception;
}
Expected behavior I expect SamlSecurityTokenHandler to fetch configuration using the ConfigurationManager and result to be valid. Microsoft.AspNetCore.Authentication.WsFederation expects the handler to fetch Configuration using the ConfigurationManager as it is done in the JsonWebTokenHandler.
Actual behavior result.IsValid == false with the exception “IDX10204: Unable to validate issuer. validationParameters.ValidIssuer is null or whitespace AND validationParameters.ValidIssuers is null or empty.”. This is because the configuration is not fetched.
Possible solution Fetch the current configuration at the start of ValidateTokenAsync and pass this with a new TokenValidationParameters to ValidateToken. I don’t know the solution for the non-async ValidateToken as GetBaseConfigurationAsync is async.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 9
- Comments: 26 (5 by maintainers)
I have worked around this issue with the following handler:
I was upgrading an old project to .Net 8 and a similar error message was returned during a login sequence. After comparing nuget references with a similar project that used .Net7, I downgraded Microsoft.AspNetCore.Authentication.WsFederation to latest 7.0.x version in my .Net 8 project and got the login to work again. How can I troubleshoot what is going on in the 8.0.x version of Microsoft.AspNetCore.Authentication.WsFederation when the error occurs?
In your AddWsFederation options add the following:
Only do the Clear(); if you know you are not going to encounter Saml2 tokens or jwt.
I’ve implemented this workaround, such as it is, from @Magnus-K-work, and it’s worked for me. I can’t say I’ve completely understood why this issue has cropped up as a result of me migrating a web app project from .NET 7 to .NET 8 (hosted on our servers, nothing to do with Azure, though we do use ADFS for authentication), but I’m hoping that at some point in the future the WSFederation pacakge might be updated to allow us to use the 8.x.x version without the need for a custom token handler.
Sorry, what’s the feature…? I can’t decide if this comment is a joke or not (I think not), but I also can’t see how adding a requirement for devs to write their own custom token handler is a feature?
@Hakon The fix for this has been included in the 7.4.1 release
@Hakon this is a new feature. I marked the issue as such.
I had similar error messages until I came up with a workaround. (Or sometimes quite different error messages, depending on experimentation with various parameters and removal of token handlers.) The messages make it look like if it all has something to do with SAML 1.0 or SAML 2.0 versions of the token, but in fact, it was missing configuration data that was the problem.
But the versions were mentioned the other way around for me – “expecting …2.0…, found …1.0…” – which makes me think that perhaps your authentication token @fabiano-ferreira is in SAML 2.0 format. In that case, you should subclass the
Saml2SecurityTokenHandlerinstead of theSamlSecurityTokenHandlerused in @Hakon’s example. That would explain why his workaround does not (yet) work for you.Hi @Hakon!
I am trying to use your workaround and now I am getting a different error:
XmlReadException: IDX30011: Unable to read XML. Expecting XmlReader to be at ns.element: '[PII of type 'System.String' is hidden. For more details, see https://aka.ms/IdentityModel/PII.].[PII of type 'System.String' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]', found: '[PII of type 'System.String' is hidden. For more details, see https://aka.ms/IdentityModel/PII.].[PII of type 'System.String' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]'. Microsoft.IdentityModel.Xml.XmlUtil.CheckReaderOnEntry(XmlReader reader, string element, string namespace) Microsoft.IdentityModel.Tokens.Saml.SamlSerializer.ReadAssertion(XmlReader reader) Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ReadSamlToken(XmlReader reader) Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ReadSamlToken(string token) Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidateSignature(string token, TokenValidationParameters validationParameters) Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidateToken(string token, TokenValidationParameters validationParameters, out SecurityToken validatedToken) Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidateTokenAsync(string token, TokenValidationParameters validationParameters)Do you have any idea about what can be happening?
We get this issue as well - I have a question to the Workaround by Hakon: Can you share the code how add CustomSamlSecurityTokenHandler?
It still doesn’t work without the CustomSamlSecurityTokenHandler workaround, at AspNetCore.Authentication.WsFederation v8.0.8.
I was updating Identity server and .Net versions, it was tricky to find what is happening.
Thank you @Hakon and @OttoG !!
After the problem was really the SAML version. Now after changing to this code, it works for me:
`public class CustomSaml2SecurityTokenHandler : Saml2SecurityTokenHandler { public override async Task<TokenValidationResult> ValidateTokenAsync(string token, TokenValidationParameters validationParameters) {
}`
Nice workaround! I did not realize that the configuration read from the
MetadataAddressset in the options was readily available to the token validation method, so that it could be picked up by a subclass that sets the missing values and then calls the base method.I came up with a workaround that will be less ideal, as it does not benefit from the built-in logic for automatically retrying configuration fetching. But it is simpler in a way and may therefore be useful to anyone who wants to verify that they suffer from the same issue, before they spend time on possibly having to troubleshoot an implementation of the above workaround.
Simply set the following two parameters in the same place where
options.MetadataAddress,options.WtRealm, etc, are set:If there are multiple valid issuers or keys, use the
ValidIssuersandIssuerSigningKeysparameters instead.I then went on to reading the settings directly from the metadata XML instead (using
new WsFederationMetadataSerializer().ReadMetadata(System.Xml.XmlReader.Create(stream))), but I would recommend trying to get @Hakon’s workaround to work, if possible. Though of course, if it is hard to find the values manually in the XML, this retrieves them in a structured format (theIssuerandSigningKeysproperties) to use when experimenting.