Call WCF authentication service using windows C #

We have a WCF service that is authenticated by Windows. Linking is performed as follows.

<basicHttpBinding>
    <binding textEncoding="utf-8" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
        <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" />            
        </security>
    </binding>
</basicHttpBinding>

I am trying to call a service from a test application like

try
{
    BasicHttpBinding binding = new BasicHttpBinding();
    binding.ReceiveTimeout = new TimeSpan(10, 10, 00);
    binding.SendTimeout = new TimeSpan(10, 10, 00);
    binding.MaxReceivedMessageSize = Int32.MaxValue;
    binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
    EndpointAddress endpoint = new EndpointAddress("ServiceUrl");

    ChannelFactory<ICRMConnectorService> channelFactory = new ChannelFactory<ICRMConnectorService>(binding, endpoint);
    channelFactory.Credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
    var service = channelFactory.CreateChannel();

    service.TestMethod();
}
catch (Exception ex)
{
    throw ex;
}

The call returns an error like, The remote server returned an error: (401) Unauthorized.

Can anybody help?

+6
source share
3 answers

You can create a client object from the ServiceReference (which you added to the application) to call methods and where you can provide Windows credentials to access the webservice.

: WCF, Windows

+1

, wcf :   <endpoint address="" binding="wsHttpBinding" contract="IService"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

, , , , ..   [OperationBehavior(Impersonation = ImpersonationOption.Required)] public void TestMethod() { }

+1

, . , , , BasicHttpsBinding. , , SSL (netsh http add sslcert ...) , , (ServicePointManager.ServerCertificateValidationCallback). post , IIS.

+1

All Articles