Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling windows authenticated WCF service from c#

We have a WCF service which is windows authenticated. Binding is configured as below.

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

I am trying to call the service from a test application as,

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 is returning an error as, The remote server returned an error: (401) Unauthorized.

Can someone please help out?

like image 658
Vijay Balkawade Avatar asked Nov 17 '17 09:11

Vijay Balkawade


People also ask

How does WCF protect message transfer from client to service?

None: WCF doesn’t protect message transfer from client to service. Signed: WCF ensures that message has come only from authenticated caller. WCF checks validity of message by checking Checksum at service side. It provides authenticity of message. Encrypted & Signed: Message is signed as well as encrypted.

How to consume a WCF service in a Windows application?

The most important task when consuming a WCF Service in a Windows application is to add the WCF Service reference into the Windows Forms application. So how to add it? Let us see the procedure. 1. Right-click on the Windows Forms application and click on "Add Service Reference" as in the following:

How to find WCF services available for a specific URL?

After pasting the URL in the preceding window box, click on the Green right headed arrow button, it will discover the WCF Services available related to that URL address and you see that in that related URL one WCF Service is found message is displayed along with the WCF Service name, "WCF Services" in the preceding right hand side window.

What is the security call context in WCF?

Every operation on a secured WCF service has a security call context. The security call context is represented by the class ServiceSecurityContext.The main use for the security call context is for custom security mechanisms, as well as analysis and auditing. ServiceSecurityContext.Current in Quickwatch window.


3 Answers

You can create a client object from ServiceReference (that you have added in your application) for calling methods and where you can provide the windows credentials to access webservice.

For practical implementation Try this: WCF Service, Windows Authentication

like image 151
Sohail xIN3N Avatar answered Oct 23 '22 09:10

Sohail xIN3N


Make sure the endpoint in the wcf service is configured something like this <endpoint address="" binding="wsHttpBinding" contract="IService"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

Make sure that the method you are calling is using impersonation i.e. [OperationBehavior(Impersonation = ImpersonationOption.Required)] public void TestMethod() { }

like image 36
user1222200 Avatar answered Oct 23 '22 07:10

user1222200


I just checked myself, with your settings the server doesn't get caller identified. I'd say you'd rather switch to another binding which is able to use secure channel, for example BasicHttpsBinding. Latter, however, requires SSL certificate set up on server (netsh http add sslcert ...), and, probably, some validation in client (ServicePointManager.ServerCertificateValidationCallback). There is also a post on the same matter, yet it involves IIS.

like image 39
Alex Seleznyov Avatar answered Oct 23 '22 09:10

Alex Seleznyov