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?
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.
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:
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.
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.
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
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()
{
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With