Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Windows身份基础验证SAML 2断言失败_C#_Saml 2.0_Wif - Fatal编程技术网

C# 使用Windows身份基础验证SAML 2断言失败

C# 使用Windows身份基础验证SAML 2断言失败,c#,saml-2.0,wif,C#,Saml 2.0,Wif,我正在尝试用WIF替换一个自定义SAML2库,我没有这方面的源代码,我没有这方面的经验。我找不到示例代码,所以我采用试错的方式。代码似乎几乎可以正常工作,但我收到一条非常简洁的签名验证失败。消息,没有其他内容 对于这个过程的输入,我有一个X509证书和一个base-64编码的SAML2断言 我的代码是 // Load X509 certificate string rootPath = System.AppDomain.CurrentDomain.BaseDirectory; string ce

我正在尝试用WIF替换一个自定义SAML2库,我没有这方面的源代码,我没有这方面的经验。我找不到示例代码,所以我采用试错的方式。代码似乎几乎可以正常工作,但我收到一条非常简洁的
签名验证失败。
消息,没有其他内容

对于这个过程的输入,我有一个X509证书和一个base-64编码的SAML2断言

我的代码是

// Load X509 certificate
string rootPath = System.AppDomain.CurrentDomain.BaseDirectory;
string certFilePath = ConfigurationManager.AppSettings["SAMLCertLocation"];
certFilePath = Path.Combine(rootPath, certFilePath);
msg = string.Format("SAMLCertLocation: [{0}]", certFilePath);
Trace.TraceInformation(msg);
X509Certificate2 cert = new X509Certificate2(certFilePath, String.Empty);

// build token handler configuration
List<System.IdentityModel.Tokens.SecurityToken> serviceTokens = new List<System.IdentityModel.Tokens.SecurityToken>();
serviceTokens.Add(new System.IdentityModel.Tokens.X509SecurityToken(cert));

ConfigurationBasedIssuerNameRegistry issuers = new ConfigurationBasedIssuerNameRegistry();
issuers.AddTrustedIssuer(cert.Thumbprint, cert.Issuer);

SecurityTokenHandlerConfiguration config = new SecurityTokenHandlerConfiguration()
{
    AudienceRestriction = { AudienceMode = AudienceUriMode.Never },
    CertificateValidator = X509CertificateValidator.None,            
    // RevocationMode = X509RevocationMode.NoCheck,             // no such property
    IssuerNameRegistry = issuers,
    MaxClockSkew = TimeSpan.FromMinutes(5),
    ServiceTokenResolver = SecurityTokenResolver.CreateDefaultSecurityTokenResolver(serviceTokens.AsReadOnly(), false)
};

// extract the assertion from the request
byte[] data = Convert.FromBase64String(reqstr);
string assertion = Encoding.UTF8.GetString(data);
Trace.TraceInformation(assertion);

// read and validate the assertion
Saml2SecurityTokenHandler handler = new Saml2SecurityTokenHandler();
handler.Configuration = config;
bool canReadToken = handler.CanReadToken(XmlReader.Create(new StringReader(assertion)));
msg = string.Format("CanReadToken [{0}]", canReadToken);
Trace.TraceInformation(msg);
if (canReadToken)
{
    try
    {
        System.IdentityModel.Tokens.SecurityToken token = handler.ReadToken(XmlReader.Create(new StringReader(assertion)));
        ClaimsIdentityCollection claims = handler.ValidateToken(token);
        msg = string.Format("SAML Claims [{0}]", claims.ToString());
        Trace.TraceInformation(msg);
    }
    catch (Exception e)
    {
        msg = string.Format("Validation Exception [{0}]", e.Message);
        Trace.TraceInformation(msg);
    }
}
断言中的签名部分如下所示

<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
  <SignedInfo>
    <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
    <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
    <Reference URI="#SAML-a964f232-77ab-40d1-a74a-f85dfe10f57d">
      <Transforms>
        <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
        <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
      </Transforms>
      <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
      <DigestValue>B9q+BrqH+Fq74R8eCqd+Vd+vKkw=</DigestValue>
    </Reference>
  </SignedInfo>
  <SignatureValue>...snip for brevity...</SignatureValue>
  <KeyInfo>
    <X509Data>
      <X509Certificate>...snip for brevity...</X509Certificate>
    </X509Data>
    <X509Data>
      <X509IssuerSerial>
        <X509IssuerName>CN=Entrust Certification Authority - L1K, OU="(c) 2012 Entrust, Inc. - for authorized use only", OU=See www.entrust.net/legal-terms, O="Entrust, Inc.", C=US</X509IssuerName>
        <X509SerialNumber>1356031830</X509SerialNumber>
      </X509IssuerSerial>
    </X509Data>
  </KeyInfo>
</Signature>

B9q+BrqH+Fq74R8eCqd+Vd+vKkw=
…为简洁起见,请剪短。。。
…为简洁起见,请剪短。。。
CN=委托认证机构-L1K,OU=“(c)2012委托有限公司-仅供授权使用”,OU=见www.trust.net/legal-terms,O=“委托有限公司”,c=US
1356031830
我想我在配置中遗漏了一些东西,所以我在中插入了基于信息的更改。然而,这并没有改变我的结果

有经验的人知道我需要添加什么才能使这项工作成功吗?

看一看

本质上,删除SAMl内容并使用OWIN Nuget Ws-Fed包

当然,这假设您的IDP支持WS-FEd?

请看一看

本质上,删除SAMl内容并使用OWIN Nuget Ws-Fed包


当然,这假设您的IDP支持WS-FEd?

WIF签名验证的一个非常恼人的问题是,它使用了不支持跟踪的SignedXml的内部实现。作为记录,public System.Security.Cryptography.Xml.SignedXml支持跟踪,因此更容易找出问题所在。无论如何,根据我的经验,我建议您检查断言xml是否包含空格。如果是,则在将其读入XmlReader时需要使用PreserveWhiteSpaces设置


编辑:需要检查的另外两件事是,是否连接了用于验证签名的正确证书,如果您可以使用System.Security.Cryptography.Xml.SignedXml验证断言的签名:

WIF签名验证的一个非常恼人的问题是,它使用了不支持跟踪的SignedXml的内部实现。作为记录,public System.Security.Cryptography.Xml.SignedXml支持跟踪,因此更容易找出问题所在。无论如何,根据我的经验,我建议您检查断言xml是否包含空格。如果是,则在将其读入XmlReader时需要使用PreserveWhiteSpaces设置


编辑:需要检查的另外两件事是,是否为验证签名连接了正确的证书,以及是否可以使用System.Security.Cryptography.Xml.SignedXml验证断言的签名:

这是WS-Fed上的“否”。此外,我还试图不干扰将断言交给我进行验证的接口。这是WS-Fed的“否”。另外,我试图不干扰交给我验证断言的接口。不要打印exception.Message only。请打印整个exception.ToString()以获取更多详细信息。基于此,也许有人能给你更多的帮助:)@Thuan-谢谢你的建议。在上面添加了新信息。您真的需要替换此模块,还是要验证IIS应用程序或类似的东西?@TGlatzer-不支持自定义模块。即使它成功了,我也不会被允许把它投入生产。问题更多:你为什么需要这样一个模块?IIS可以为您完成所有繁重的工作。不要打印异常。仅显示消息。请打印整个exception.ToString()以获取更多详细信息。基于此,也许有人能给你更多的帮助:)@Thuan-谢谢你的建议。在上面添加了新信息。您真的需要替换此模块,还是要验证IIS应用程序或类似的东西?@TGlatzer-不支持自定义模块。即使它成功了,我也不会被允许把它投入生产。问题更多:你为什么需要这样一个模块?IIS可以帮你做所有的重担。这么简单。我遵循SignedXml示例。5行代码并完成。谢谢大家!+1指出空白对于签名验证非常重要
XmlReader
默认为剥离空白,因此请确保在
XmlReaderSettings
上设置
IgnoreWhitespace=true
。这么简单。我遵循SignedXml示例。5行代码并完成。谢谢大家!+1指出空白对于签名验证非常重要
XmlReader
默认为剥离空白,因此请确保在
XmlReaderSettings
上设置
IgnoreWhitespace=true
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
  <SignedInfo>
    <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
    <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
    <Reference URI="#SAML-a964f232-77ab-40d1-a74a-f85dfe10f57d">
      <Transforms>
        <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
        <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
      </Transforms>
      <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
      <DigestValue>B9q+BrqH+Fq74R8eCqd+Vd+vKkw=</DigestValue>
    </Reference>
  </SignedInfo>
  <SignatureValue>...snip for brevity...</SignatureValue>
  <KeyInfo>
    <X509Data>
      <X509Certificate>...snip for brevity...</X509Certificate>
    </X509Data>
    <X509Data>
      <X509IssuerSerial>
        <X509IssuerName>CN=Entrust Certification Authority - L1K, OU="(c) 2012 Entrust, Inc. - for authorized use only", OU=See www.entrust.net/legal-terms, O="Entrust, Inc.", C=US</X509IssuerName>
        <X509SerialNumber>1356031830</X509SerialNumber>
      </X509IssuerSerial>
    </X509Data>
  </KeyInfo>
</Signature>