如何使用.net webservices wse或wcf创建安全的SOAP主体?

时间:2016-01-15 03:10:03

标签: c# .net wcf security soap

目前我正在使用.NEt的Java Web服务。在这里,我使用WSE 3.0来使用Java安全Web服务。但我得到了错误:

  

“抛出异常:无法检索引用的安全令牌”

在我比较java SOAP Request和.NET SOAP Request之后,我发现我发送了正确的.net SOAP标题,但是我发错了。任何人都可以帮助我如何发送安全的身体申请表.net客户端

下面,我给出了Java和.Net SOAP XML数据:

.Net请求XML SOAP Body(我发送的内容)

<soap:Body wsu:Id="Id-165fc268-5917-43b9-aed2-091fb948c508">
          <xenc:EncryptedData Id="Enc-20dbd181-a655-4843-882a-b8b36b9d028d" Type="http://www.w3.org/2001/04/xmlenc#Content" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
            <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
            <xenc:CipherData>
              <xenc:CipherValue>UO32nmFhQZ4JBNuWk6xTz20FpjfPWQqr1F5zqOf7uQ6hBJzfndGAJvb/l/MgT0x7P2ZTiEeNj51ZDYKDZrQ1Ax3SCJyzacX6suemUVWmMaVGtJ8DJPqka7T3xDkWpgVlDmc1am1B+E7SXdfd9RIINv+JpYhF5Fx4m3ZaeYvPQLVrvF3Rpvya2L1mC/LeHVYwM/ep6x5f9tQnz50UASBHIA==</xenc:CipherValue>
            </xenc:CipherData>
          </xenc:EncryptedData>
        </soap:Body>

JAVA请求的XML SOAP Body(预计来自java Web服务器)

      <soap:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-2">
<xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Id="ED-5" Type="http://www.w3.org/2001/04/xmlenc#Content">
<xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc"/>
<ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">

<wsse:SecurityTokenReference xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsse11="http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd" wsse11:TokenType="http://docs.oasis-open.org/wss/oasis-wss-soap-message-security-1.1#EncryptedKey">
<wsse:Reference URI="#EK-7FBF3DBE856BC8B2BC14527661038314"/>
</wsse:SecurityTokenReference>
</ds:KeyInfo>
<xenc:CipherData>
<xenc:CipherValue>SQ0nnx45EPDL5uoFRzeTjJy6+FhaCZTK/3LRz5DrPM4qAQ2JzARNiLmuj61YSHd6nOMVy1QmPCqH5gG6PIIN8x47r10fzOkuisxpcOaUdnFL3bY55AvfyL6fUbSfcp+fl3qw6SAB3QF0AR1thqpfKBttBv8b7GxbpApCZg6TWaw8nD7G7dVmtdpDBJN7uQSQJu5ibdBGLzbVoF9YtliYH1mbdswL4KVJtZKUl2UAQqDtbxgXAkKtNwNyq4pt7N+HVhX00mZMxiTE0IyRyfgQhwp6afsTvsGVmdKxcpWtRoOFmmIHhrTeXJal/jJAI84mmg5EV44TJezFFbEyqsL+vhhR/N6oITaa
</xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedData>
</soap:Body>

关于如何从.Net Web服务或WCF向服务器发送与Java相同的请求的任何想法?

2 个答案:

答案 0 :(得分:0)

您必须执行类似的操作才能将X.509集成到您的Web服务客户端。

       public void MyWebServiceClient()
       {
            using (var client = new MyWebService())
            {
                try
                {
                    //calls the web service
                    client.Url = //Your server EndpointUri;

                    //assign cert
                    ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
                    string certificatePath = //certificate path
                    string certificatePassword = //certificate password
                    X509Certificate2 cert = new X509Certificate2(certificatePath, certificatePassword, X509KeyStorageFlags.MachineKeySet);
                    client.ClientCertificates.Add(cert);

                    //var result = client.WebServiceCall(your input);
                }
                catch (Exception ex)
                {
                    throw new Exception("Error " + ex.Message);
                }
            }
        }


    public bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
    {            
        // accept all certificates
        return true;
    }

答案 1 :(得分:0)

    private void button1_Click(object sender, EventArgs e)
    {
     MyJavaService myClient = new MyJavaService();

   X509SecurityToken signatureToken = GetSecurityToken("CN=Clientcer, C=MY");


           if (signatureToken == null)
           {
               throw new SecurityFault("Message Requirements could not be satisfied.");
           }
           SoapContext requestContext = myClient.RequestSoapContext;

            requestContext.Security.Tokens.Add(signatureToken);


            MessageSignature sig = new MessageSignature(signatureToken);
           requestContext.Security.Elements.Add(sig);

             EncryptedData myEncData = new EncryptedData(signatureToken);

           requestContext.Security.Elements.Add(myEncData);

            envelope.Context.Security.Elements.Add(sig);


            //calling method
             string clientversion = "";
            string status = "";

            myClient.SetPolicy("MyClient");
             myClient.GetVersionOfClient(ref clientversion, ref status);

             //if i get correct responce i can get the values of clientversion,status from webserver
             string a=clientversion;  
             string b=status;


        }


 public X509SecurityToken GetSecurityToken(string subjectName)
    {
        X509SecurityToken objX509SecurityToken = null;
        X509Store objX509Store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        objX509Store.Open(OpenFlags.ReadOnly);

        try
        {
            X509Certificate2Collection objX509Certificate2Collection = objX509Store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, subjectName, true);

            X509Certificate2 objX509Certificate2;

            if (objX509Certificate2Collection.Count == 1)
            {
                objX509Certificate2 = objX509Certificate2Collection[0];
                objX509SecurityToken = new X509SecurityToken(objX509Certificate2);
            }
            else
            {
                objX509SecurityToken = null;
            }
        }
        catch (Exception ex)
        {
            objX509SecurityToken = null;
        }
        finally
        {
            if (objX509Store != null)
                objX509Store.Close();
        }

        return objX509SecurityToken;
    }

嗨kosala这是我的代码。我需要做的任何改变? ..我正在使用Windows窗体应用程序来使用webservices