LUKE
LUKE

Reputation: 128

WCF Client - connect to service

I have problem connection to wcf service over https. I didn't created wcf service. In Internet Explorer when I set url end open page, ask me to write username and password.

I opend new C# project, wanted to add new service reference but each time I get error:

There was an error downloading 'https://address/path/service.svc/_vti_bin/ListData.svc/$metadata'. The underlying connection was closed: An unexpected error occurred on a send. Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. An existing connection was forcibly closed by the remote host Metadata contains a reference that cannot be resolved: 'https://address/path/service.svc.svc'. An error occurred while making the HTTP request to https://address/path/service.svc.svc. This could be due to the fact that the server certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be caused by a mismatch of the security binding between the client and the server. The underlying connection was closed: An unexpected error occurred on a send. Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. An existing connection was forcibly closed by the remote host If the service is defined in the current solution, try building the solution and adding the service reference again.

I also tried to download wsdl file and with add service reference added this local wsdl file. It passed but now when I start client I got exception:

The username is not provided. Specify username in ClientCredentials.

try
{
    ServiceReference1.IService stub = new ServiceReference1.ServiceClient();
    stub.calculate("test");
}
catch (Exception ee)
{
}

but I don't have options of ClientCredentials in stub object???

app.config

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/></startup>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService">
                <security mode="Transport">
                    <transport clientCredentialType="Basic" proxyCredentialType="Basic" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="https://address/path/service.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
            contract="ServiceReference1.IService" name="BasicHttpBinding_IService" />
    </client>
</system.serviceModel>

How can I solve this problem??

Upvotes: 1

Views: 1331

Answers (2)

LUKE
LUKE

Reputation: 128

I put before calling method:

                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;

Code:

            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;            //System.Net.ServicePointManager.
        try
        {
            var stub = new ServiceReference1.ServiceClient();
            stub.ClientCredentials.UserName.UserName = "user";
            stub.ClientCredentials.UserName.Password = "pass";
            //stub.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.PeerOrChainTrust;
            //System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;

            stub.calculate("Test");
        }
        catch (Exception ee)
        {
        }

Upvotes: 1

Renats Stozkovs
Renats Stozkovs

Reputation: 2605

ServiceClient by definition has a property called ClientCredentials, although IService does not. Maybe try to change your variable type from IService to ServiceClient?

ServiceReference1.ServiceClient stub = new ServiceReference1.ServiceClient();
stub.ClientCredentials.UserName.UserName = username;  
stub.ClientCredentials.UserName.Password = password;  

// Treat the test certificate as trusted  
stub.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.PeerOrChainTrust;  

Here is a link to MSDN that shows how to authenticate with username / password

Upvotes: 1

Related Questions