Tuesday, July 7, 2009

Creating WCF client for asmx wervice and passing default Credential

If you are consuming a asmx service as a WCF client and try to pass the credential to the service you may recieve following error.

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate,NTLM

Reason : WCF uses anonymous authentication, whereas the ASP.Net development web server uses NTLM.

To avoid This error you have to spcify security configuration for wcf client in app.config of client


<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="WebsSoap" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />


<!--keey come here you have to spcify mode as TransportCredentialOnly -->
<security mode="TransportCredentialOnly">
<!--keey come here you have to spcify the clientCredentialType as Ntlm-->
<transport clientCredentialType="Ntlm"/>
</security>
</binding>
</basicHttpBinding>
<client>
<endpoint address="http://litwaredemo:28519/_vti_bin/webs.asmx"
binding="basicHttpBinding" bindingConfiguration="WebsSoap"
contract="SharePointService.WebsSoap" name="WebsSoap" />
</client>
</system.serviceModel>


How to pass Credential

SampleServiceClient sourceService = new SampleServiceClient ();

sourceService.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

sourceService.ClientCredentials.Windows.ClientCredential = (NetworkCredential) System.Net.CredentialCache.DefaultCredentials;


Cheers
Shyju Mohan

No comments:

Post a Comment