Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to connect to the WebService with C# but I cannot attach certificate to the request. I succesfully authorized myself using SoapUI so I'm sure something is wrong with my code, not the WS.

First thing I done was generating proxy class with delivered WSDL file (using Add Service Reference option in Visual Studio). In my Web.config file I got code like this:

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="myServiceSoapBinding" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://xxxxxxxxxxxxxxx/myService"
    binding="basicHttpBinding" bindingConfiguration="myServiceSoapBinding"
    contract="xxxxxxxxxxxxx.myService" name="myServicePort" />
</client></system.serviceModel>

Then, according to that i wrote this code:

var basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Message);
basicHttpBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;
var endpoint = new EndpointAddress("http://xxxxxxxxxxxxxxx/myService");
myService.myServiceClient myServiceClient = new myService.myServiceClient(basicHttpBinding, endpoint);
var cert = new X509Certificate2(@"xxxxxxkey.p12", "password");
myServiceClient.ClientCredentials.ClientCertificate.Certificate = cert;
myServiceClient.Endpoint.Behaviors.Add(new DebugMessageBehavior());
myService.NewOrderRequest = new myService.NewOrderRequest();
      ....
var result = myServiceClient.makeNewOrder(orderRequest);

Finally, I checked whole request and noticed, there is no wsse:Security tag. There is request I was about to send:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none" />
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <newOrderRequest xmlns="http://xxxxxxxxxxxxx/">
      <requestHeader xmlns="">
        <!--some params here-->
      </requestHeader>
    </newOrderRequest>
  </s:Body>
</s:Envelope>

Hours of "making changes and testing" ago I was able to sucesfully send the request but WS replied that my certifcate is incorrect or missing. At this moment I'm just getting an exception

The service certificate is not provided for target 'xxxxxxxx'. Specify a service certificate in ClientCredentials.

I stuck here and I have no more ideas. Of course I read a lot of similiar topic but nothing of them solve my problem. Can you help me?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
646 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...