我正在尝试将 SAML2 IdP 配置Salesforce
为 IdentityServer3 中的外部提供程序。我正在使用SustainSys/Saml2库。因此,出于测试目的,我下载了SampleIdentityServer3。并配置 SAML2 IdP,如下所示
private void ConfigureSaml2(IAppBuilder app, string signInAsType)
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
var options = new Saml2AuthenticationOptions(false)
{
SPOptions = new SPOptions
{
EntityId = new EntityId("http://localhost:4589/IdSrv3/Saml2"),
MinIncomingSigningAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1"
},
SignInAsAuthenticationType = signInAsType,
Caption = "SAML2p",
};
UseIdSrv3LogoutOnFederatedLogout(app, options);
options.SPOptions.ServiceCertificates.Add(new X509Certificate2(
AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "/App_Data/Sustainsys.Saml2.Tests.pfx"));
var idp = new IdentityProvider(
new EntityId("https://XXXXXX-dev-ed.my.salesforce.com"),
options.SPOptions)
{
MetadataLocation = "https://XXXXXX-dev-ed.my.salesforce.com/.well-known/samlidp.xml",
LoadMetadata = true,
};
options.IdentityProviders.Add(idp);
app.UseSaml2Authentication(options);
}
请注意,如果我没有设置MinIncomingSigningAlgorithm
为 sh1,那么 SustainSys 库会引发错误。
Sustainsys.Saml2.Exceptions.InvalidSignatureException:签名算法http://www.w3.org/2000/09/xmldsig#rsa-sha1弱于最低接受 http://www.w3.org/2001/04/ xmldsig-more#rsa-sha256。如果要允许此签名算法,请使用 minIncomingSigningAlgorithm 配置属性。
所以我设置为MinIncomingSigningAlgorithm
摆脱"http://www.w3.org/2000/09/xmldsig#rsa-sha1"
错误。
但后来我得到不同的错误
Sustainsys.Saml2.Exceptions.InvalidSignatureException:使用签名中包含的密钥正确验证了签名,但该密钥不受信任。
根据问题#493 #735,元数据中的证书必须与 SAML2 响应中的证书匹配。
在元数据中,证书是(注意开始和结束值)
<ds:X509Data>
<ds:X509Certificate>
MIIGk... removed from brevity....tmv6J1g==
</ds:X509Certificate>
</ds:X509Data>
但在 SAML2 响应中(由 SustainSys 库记录的响应)
<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" Destination="http://localhost:4589/IdSrv3/Saml2/Acs" ID="_19fd2d8d9aab0401f56fXXXXXXXXX" InResponseTo="id473a52c49f194bXXXXXXXXX" IssueInstant="2018-08-27T20:10:04.296Z" Version="2.0">
<saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity">https://XXXXXXX-dev-ed.my.salesforce.com</saml:Issuer>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<ds:Reference URI="#_19fd2d8d9aab0401f56f642dXXXXXXXXXXXXX">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"><ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="ds saml samlp" /></ds:Transform>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<ds:DigestValue>fQiiyd0T57Ztr5BAfMFe9MTrhY0=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>
B6hndlsBgY45J+hm8My2gPVo....removed for brevity....YT88ajt7jQ==
</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>
MIIENz... remove for brevity....y2Ul24Jyc4V/jJN
</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
</ds:Signature>
<samlp:Status>
<samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:AuthnFailed" />
</samlp:Status>
</samlp:Response>
查看X509Certificate
元数据和 SAML2 响应中的值,它们不匹配。
问题SAML2 响应中
的值是否应该与元数据中的值匹配?如果是,为什么 SustainSys 库不能始终使用SAML2 响应中的值?X509Certificate
X509Certificate
X509Certificate
更新
只是为了看看匹配值是否有效,我将 SAML2 响应中的证书值保存到单独的.cer
文件中。然后在 KeyInfoSerializer.cs 文件中我更新ReadX509Certificate
了方法(这是从元数据加载证书的方法)
private static SecurityKeyIdentifierClause ReadX509Certificate(XmlReader reader)
{
reader.ReadStartElement("X509Certificate", SignedXml.XmlDsigNamespaceUrl);
((XmlDictionaryReader)reader).ReadContentAsString();
var cer = new X509Certificate2(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "/App_Data/salesforcepublickey.cer");
var clause = new X509RawDataKeyIdentifierClause(cer);
reader.ReadEndElement();
return clause;
}
但是,它仍然会引发错误The signature verified correctly with the key contained in the signature, but that key is not trusted.