Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to connect to a WCF service with the following configuration:

XML
<wsp:Policy wsu:Id="WSHttpBinding_IService_policy">
    <wsp:ExactlyOne>
        <wsp:All>
            <wsoma:OptimizedMimeSerialization xmlns:wsoma="http://schemas.xmlsoap.org/ws/2004/09/policy/optimizedmimeserialization"/>
            <sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
                <wsp:Policy>
                    <sp:TransportToken>
                        <wsp:Policy>
                            <sp:HttpsToken RequireClientCertificate="false"/>
                        </wsp:Policy>
                    </sp:TransportToken>
                    <sp:AlgorithmSuite>
                        <wsp:Policy>
                            <sp:Basic256/>
                        </wsp:Policy>
                    </sp:AlgorithmSuite>
                    <sp:Layout>
                        <wsp:Policy>
                            <sp:Strict/>
                        </wsp:Policy>
                    </sp:Layout>
                    <sp:IncludeTimestamp/>
                </wsp:Policy>
            </sp:TransportBinding>
            <sp:EndorsingSupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
                <wsp:Policy>
                    <sp:SecureConversationToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">
                        <wsp:Policy>
                            <sp:BootstrapPolicy>
                                <wsp:Policy>
                                    <sp:SignedParts>
                                        <sp:Body/>
                                        <sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing"/>
                                        <sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing"/>
                                        <sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing"/>
                                        <sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing"/>
                                        <sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing"/>
                                        <sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing"/>
                                        <sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing"/>
                                    </sp:SignedParts>
                                    <sp:EncryptedParts>
                                        <sp:Body/>
                                    </sp:EncryptedParts>
                                    <sp:TransportBinding>
                                        <wsp:Policy>
                                            <sp:TransportToken>
                                                <wsp:Policy>
                                                    <sp:HttpsToken RequireClientCertificate="false"/>
                                                </wsp:Policy>
                                            </sp:TransportToken>
                                            <sp:AlgorithmSuite>
                                                <wsp:Policy>
                                                    <sp:Basic256/>
                                                </wsp:Policy>
                                            </sp:AlgorithmSuite>
                                            <sp:Layout>
                                                <wsp:Policy>
                                                    <sp:Strict/>
                                                </wsp:Policy>
                                            </sp:Layout>
                                            <sp:IncludeTimestamp/>
                                        </wsp:Policy>
                                    </sp:TransportBinding>
                                    <sp:SignedSupportingTokens>
                                        <wsp:Policy>
                                            <sp:UsernameToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">
                                                <wsp:Policy>
                                                    <sp:WssUsernameToken10/>
                                                </wsp:Policy>
                                            </sp:UsernameToken>
                                        </wsp:Policy>
                                    </sp:SignedSupportingTokens>
                                    <sp:Wss11>
                                        <wsp:Policy/>
                                    </sp:Wss11>
                                    <sp:Trust10>
                                        <wsp:Policy>
                                            <sp:MustSupportIssuedTokens/>
                                            <sp:RequireClientEntropy/>
                                            <sp:RequireServerEntropy/>
                                        </wsp:Policy>
                                    </sp:Trust10>
                                </wsp:Policy>
                            </sp:BootstrapPolicy>
                        </wsp:Policy>
                    </sp:SecureConversationToken>
                </wsp:Policy>
            </sp:EndorsingSupportingTokens>
            <sp:Wss11 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
                <wsp:Policy/>
            </sp:Wss11>
            <sp:Trust10 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
                <wsp:Policy>
                    <sp:MustSupportIssuedTokens/>
                    <sp:RequireClientEntropy/>
                    <sp:RequireServerEntropy/>
                </wsp:Policy>
            </sp:Trust10>
            <wsaw:UsingAddressing/>
        </wsp:All>
    </wsp:ExactlyOne>
</wsp:Policy>
<wsdl:types>


I tried to connect but the service returns the following error:

System.ServiceModel.Security.MessageSecurityException: Incorrectly unprotected or unprotected error received on the other side. For the error code and further details, see Internal FaultException. ---> System.ServiceModel.FaultException: Unable to validate one or more security tokens in the message.


What I have tried:

App.comfig

<system.serviceModel>
   <bindings>
     <wsHttpBinding>
       <binding name="WSHttpBinding_IService" messageEncoding="Mtom">
         <security mode="TransportWithMessageCredential">
           <transport clientCredentialType="None"/>
           <message clientCredentialType="UserName"/>
         </security>
       </binding>
     </wsHttpBinding>
   </bindings>
   <client>
     <endpoint address="https://*****/Service.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService" contract="SaniarpServices.IService" name="WSHttpBinding_IService"/>
   </client>
 </system.serviceModel>


Code extract:
C#
try
            {
                var c = new Services.ServiceClient();

                var headers = new List<XmlQualifiedName>();
                headers.Add(new XmlQualifiedName("Action", "http://www.w3.org/2005/08/addressing"));
                headers.Add(new XmlQualifiedName("MessageID", "http://www.w3.org/2005/08/addressing"));
                headers.Add(new XmlQualifiedName("ReplyTo", "http://www.w3.org/2005/08/addressing"));
                headers.Add(new XmlQualifiedName("To", "http://www.w3.org/2005/08/addressing"));
                headers.Add(new XmlQualifiedName("From", "http://www.w3.org/2005/08/addressing"));                               

                c.Endpoint.Behaviors.Add(
                     new SignMessageHeaderBehavior(headers, "http://tempuri.org/IService/VerificaCF"));

                c.ClientCredentials.UserName.UserName = "test@tin.it";

                Result ra = c.CheckCode("123");

                if (ra.State == MessageStatusType.OK)
                {
                    tbText.Text = "Connection ok: " + ra.TextState;
                }
                else
                {
                    tbText.Text = "Connection ko: " + ra.TextState;
                }
            }
            catch (Exception ex)
            {
                tbText.Text = ex.ToString();
            }
Posted
Updated 20-Jun-17 2:04am

Could be caused by HTTPS, what happens if you use HTTP ?
Did you use the correct port ? I had a problem on Win 10 with the default port, had to change it to a port number > 8000 before it would work.
Did you configure the firewall to allow connections ?
 
Share this answer
 
v2
Comments
antann78 19-Jun-17 16:58pm    
First of all thanks for the help.

Could be caused by HTTPS, what happens if you use HTTP ?

I have to use https because the service is not mine.

Did you use the correct port ?
I had a problem on Win 10 with the default port, had to change it to a port number > 8000 before it would work.

In the specifications I did not have the port but wsdl I can connect.

Did you configure the firewall to allow connections ?

There is no firewall on my terminal and in my network.
Hello,

      I solved the problem.

In fact, they had informed me of the incorrect information because the code was correct but you have to provide credentials recognized by the service.

I trivially solved this as follows:
C#
try
{
                var c = new Services.ServiceClient();

                c.ClientCredentials.UserName.UserName = "test@tin.it";
                c.ClientCredentials.UserName.Password= "pass";

                Result ra = c.CheckCode("123");

                if (ra.State == MessageStatusType.OK)
                {
                    tbText.Text = "Connection ok: " + ra.TextState;
                }
                else
                {
                    tbText.Text = "Connection ko: " + ra.TextState;
                }
}
catch (Exception ex)
{
    tbText.Text = ex.ToString();
}


Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900