使用x509证书请求

时间:2018-06-26 22:23:47

标签: c# xml security xml-signature

我需要创建一个wcf客户端来调用我无法控制的服务。

我得到了一个wsdl和一个有效的soapui项目。

请求需要使用x509证书签名 }我用来创建签名块的代码是

        XmlNamespaceManager ns = new XmlNamespaceManager(xmlDoc.NameTable);
        ns.AddNamespace("soapenv","http://schemas.xmlsoap.org/soap/envelope/");

        XmlElement body = xmlDoc.DocumentElement.SelectSingleNode(@"//soapenv:Body", ns) as XmlElement;
        if (body == null)
            throw new ApplicationException("No body tag found");
        body.SetAttribute("id", "Body");
        SignedXml signedXml = new SignedXml(xmlDoc);


        System.Security.Cryptography.Xml.KeyInfo keyInfo = new System.Security.Cryptography.Xml.KeyInfo();
        signedXml.SigningKey = cert.PrivateKey;

        XmlElement x = xmlDoc.CreateElement("wsse", "SecurityTokenReference", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
        XmlElement y = xmlDoc.CreateElement("wsse", "Reference", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
        y.SetAttribute("URI", "#X509-3B3DFD3FDCB88C6080153003319258849");
        y.SetAttribute("ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1");
        x.AppendChild(y);
        var keyInfoData = new KeyInfoNode(x);
        keyInfo.AddClause(keyInfoData);
        signedXml.KeyInfo = keyInfo;

        signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;

        System.Security.Cryptography.Xml.Reference reference = new System.Security.Cryptography.Xml.Reference();
        reference.Uri = "#Body";

        reference.AddTransform(new XmlDsigExcC14NTransform());
        signedXml.AddReference(reference);
        signedXml.ComputeSignature();

        XmlElement signedElement = signedXml.GetXml();
        signedElement.Prefix = "ds";

        return signedElement.OuterXml;

但是,我可以获得成功响应,这部分xml来自成功请求

 <ds:Signature Id="SIG-3B3DFD3FDCB88C6080153003319258852" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
<ec:InclusiveNamespaces PrefixList="#default xsd xsi" xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#"/>
</ds:CanonicalizationMethod>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference URI="#ID-6596bbaa-9256-4123-828c-f222152e3e6a">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
<ec:InclusiveNamespaces PrefixList="xsd xsi" xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#"/>
</ds:Transform>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>9ABqQFoUAXhjkhjkgEfABGs=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<SignatureValue>....</SignatureValue>
<ds:KeyInfo Id="KI-3B3DFD3FDCB88C6080153003319258850">
<wsse:SecurityTokenReference wsse11:TokenType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1" wsu:Id="STR-3B3DFD3FDCB88C6080153003319258851" xmlns:wsse11="http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd">
<wsse:Reference URI="#X509-3B3DFD3FDCB88C6080153003319258849" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1"/>
</wsse:SecurityTokenReference>
</ds:KeyInfo>
</ds:Signature>

我在生成关键信息部分和摘要时遇到了一些问题,希望有人可以正确地指导我,提前进行思考

0 个答案:

没有答案