Consume WCF with BasicHttpBinding and Windows Authentication - wcf

I have a WCF Service, using the basichttpbinding and windows authentication (no anonymous),
The WCF service is hosted in the domain server
I need to consume the service from a server which is out of the domain, that server doesn't join domain, that is a workgroup. Is that possible? please help for a code snippet.
The configuration like below
<bindings>
<basicHttpBinding>
<binding name="defaultBinding" receiveTimeout="01:00:00" sendTimeout="01:00:00" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" />
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="defaultServiceBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="defaultServiceBehavior" name="namespace.MyService">
<endpoint binding="basicHttpBinding" bindingConfiguration="defaultBinding" contract="namespace.IMyService">
<identity>
<dns value="abc.xyz.com"/>
</identity>
</endpoint>
</service>
</services>
Edited: here is my client code
_securityServiceClient = new SecurityServiceClient();
_securityServiceClient.ClientCredentials.UserName.UserName = "lto3";
_securityServiceClient.ClientCredentials.UserName.Password = "Lydowe#1982";
I got this error
An exception of type 'System.ServiceModel.Security.MessageSecurityException' occurred in mscorlib.dll but was not handled in user code
Additional information: The HTTP request is unauthorized with client
authentication scheme 'Basic'. The authentication header received from
the server was 'Negotiate,NTLM'.

Try using:
_securityServiceClient.ClientCredentials.Windows.ClientCredential =
new NetworkCredential("lto3", "Ludowe#1982");
As per the msdn documentation ClientCredentials.Windows is how you specify the credentials to use when using windows authentication.

Related

Moving WCF service to IIS6 with SSL enabled

I ran my WCF service on the server without SSL enabled and now I moved it to one with SSL enabled and I am getting the following error:
Could not find a base address that matches scheme http for the endpoint with binding BasicHttpBinding. Registered base address schemes are [https].
Below are my settings:
<bindings>
<basicHttpBinding>
<binding name="basicHTTP">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows">
</transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="basicBehavior" name="ProjectName.MyService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHTTP" contract="ProjectName.IMyService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="basicBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
How can I fix this error?
You need to define a binding for basicHttps. This is a very simple settings that works for SSL:
<system.serviceModel>
<bindings>
<basicHttpsBinding>
<binding name="BasicHttpBinding_IMyService" />
</basicHttpsBinding>
</bindings>
<client>
<endpoint
name="BasicHttpBinding_IMyService"
address="https://MyURL/MyService.svc/soap/"
binding="basicHttpsBinding"
bindingConfiguration="BasicHttpBinding_IMyService"
contract="ClientServiceReference.IMyService" />
</client>
</system.serviceModel>
Note: The endpoint needs to be defined and its URL is https.
Also, make sure that in production environment, you are not sending the exception details back to the caller (that would be considered a security hole in your system because exceptions can reveal too much information to hackers). You must change this line:
<serviceDebug includeExceptionDetailInFaults="false"/>
Fixed the issue by specifying security mode as Transport and using webHttpBinding

Unable to add service reference

I am unable to connect to a WCF service hosted on our dev server but I am able to browse to it.
I am just trying to add a service reference via Visual Studio and when I click on Go, it gives me the following error.
An error occurred while receiving the HTTP response to http://...Service.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.
My other colleagues are able to add the service reference and connect to this service with no issues, which makes it harder for me to understand the cause of this issue.
Web.config - system.serviceModel:
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding" maxReceivedMessageSize="2147483646">
<readerQuotas maxStringContentLength="2147483646" />
<security mode="None">
<!-- **WARNING** Changes to the security binding must also be made in client binding code -->
<transport clientCredentialType="None" proxyCredentialType="None" />
<message clientCredentialType="None" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="X.X.X.RepositoryService" behaviorConfiguration="repositoryServiceBehavior">
<endpoint address=""
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding"
contract="X.X.X.ICatalogServiceContract" />
<!-- ** NOTE Metadata not supported SSL at this time **-->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="repositoryServiceBehavior">
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
</behavior>
</serviceBehaviors>
</behaviors>

How to consume HTTPS service having following binding configurations?

Following is how the serviceModel configured in WCF's web.config:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basicHttpWithSecure">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic"></transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="CMA.Customers">
<endpoint name="basic" binding="basicHttpBinding" contract="CMA.Customers" bindingConfiguration="basicHttpWithSecure"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpsGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
This service is deployed on IIS, and I can access it using the url -
https://www.companyname.co.uk/sitename/wcfservicename/service.svc
Authentication is set to "Anonymous", and "Basic" in IIS for this WCF.
Now I have an MVC application deployed on the same site, and I want to point that MVC site to consume this WCF. How can I configure it in web.config of my MVC site?
Or you think any change required in WCF's configurations to make it possible?
change your security mode :
<security mode="TransportCredentialOnly">
to
<security mode="Transport">
because TransportCredentialOnly is for http-based client authentication.

WCF REST Service not visible in WCFTestClient

I have successfully configured 3 endpoints for my prototype service. The endpoints are basicHttpBinding, wsHttpBinding, and webHttpBinding. The only glitch I have at the moment is in the WCFTestClient. When I point it to my service it lists the first two, but not the webHttpBinding. I can test the REST endpoint through my browser and it works just fine. Here's my config:
<system.serviceModel>
<services>
<service behaviorConfiguration="serviceBehaviour" name="VMDServices.VMDService">
<endpoint binding="webHttpBinding"
address="rest" behaviorConfiguration="webBehaviour" contract="VMDServices.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint binding="basicHttpBinding"
address="basic" bindingConfiguration="basicBinding" contract="VMDServices.IService1">
</endpoint>
<endpoint binding="wsHttpBinding"
address="ws" bindingConfiguration="wsBinding" contract="VMDServices.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="basicBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<security mode="None"></security>
<readerQuotas maxStringContentLength="2147483647"/>
</binding>
</basicHttpBinding>
<wsHttpBinding>
<binding name="wsBinding" transactionFlow="true">
<security mode="None"></security>
<reliableSession enabled="true" ordered="true" />
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="serviceBehaviour">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true">
</serviceHostingEnvironment>
</system.serviceModel>
Is there any reason why I can't see the webHttpEndpoint in the WCFTestClient tool?
Cheers,
Dany.
It's because web endpoints (unlike SOAP ones) do not expose metadata, so the test client doesn't know about it when it downloads the WSDL for the service. Unlike SOAP, which has well-defined formats for exposing metadata (WSDL, MEX), web (a.k.a. REST) endpoints do not.
That's the short story. If you want to know more details, I wrote a blog post about it at http://blogs.msdn.com/b/carlosfigueira/archive/2012/03/26/mixing-add-service-reference-and-wcf-web-http-a-k-a-rest-endpoint-does-not-work.aspx
The following is a list of features not supported by WCF Test Client:
• Types: Stream, Message, XmlElement, XmlAttribute, XmlNode, types that implement the
IXmlSerializableinterface, including the related XmlSchemaProviderAttribute attribute, and the XDocument and XElement types and the ADO.NET DataTable type.
• Duplex contract.
• Transaction.
• Security: CardSpace , Certificate, and Username/Password.
• Bindings: WSFederationbinding, any Context bindings and Https binding, WebHttpbinding (Json response message support).
Source: http://msdn.microsoft.com/en-us/library/bb552364.aspx
try adding the "mexHttpBinding" endpoint that exposes the metadata

Setting Max Message and buffer size for WCF webhttp

I currently have a WCF service with webHttp bindings, im attempting to increase the max size that can be inputted to the service by overriding the default settings in config, i have tried doing something like
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttp" >
<security mode="Transport">
<transport clientCredentialType =
"None"
proxyCredentialType="None"
realm="string" />
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="PrimeStreamInfoServices.Service1" behaviorConfiguration="PrimeStreamInfoServices.Service1Behavior">
<!-- Service Endpoints -->
<endpoint address="" binding="webHttpBinding" contract="PrimeStreamInfoServices.IService1">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="PrimeStreamInfoServices.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<diagnostics>
and setting various other properties pertaining to message size but none seems to be working, can one even change the m essage size of a webHttp binding?
Any suggestions? Thanks!
There's a multitude of settings that might have an influence depending on your settings - try this:
<bindings>
<webHttpBinding>
<binding name="LargeWeb"
maxBufferPoolSize="1500000"
maxReceivedMessageSize="1500000"
maxBufferSize="1500000">
<readerQuotas
maxArrayLength="656000"
maxBytesPerRead="656000"
maxDepth="32"
maxNameTableCharCount="656000"
maxStringContentLength="656000"
/>
</binding>
</webHttpBinding>
</bindings>
By defining your "version" of the webHttpBinding and setting all those parameters to higher values, you should be able to get through any message size (almost).
Mind you: this does open up your system to the potential of being flooded with huge messages and thus be brought down to its knees (classic denial-of-service attacks) - that's the reason these limits are set fairly low - by design and on purpose.
You can change them to higher values - just be aware what you're doing and what the security risks are, if you do!
Marc
PS: In order to make use of these settings, you of course have to reference that binding configuration in your server and client side configs:
<client>
<endpoint address="http://localhost"
binding="webHttpBinding" bindingConfiguration="LargeWeb"
contract="IMyService" />
</client>
<services>
<service>
<endpoint address="http://localhost"
binding="webHttpBinding" bindingConfiguration="LargeWeb"
contract="IMyService" />
</service>
</services>
Setting Max Message and buffer size for WCF Rest services webhttpbinding
<bindings>
<webHttpBinding>
<binding maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="200" maxStringContentLength="83886089" maxArrayLength="163841" maxBytesPerRead="2147483647" maxNameTableCharCount="16384"/>
</binding>
</webHttpBinding>
</bindings>
If you are using [DataContract] decorator in your model, you need to add yhe dataContractSerializer into your web.config
Example:
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpsGetEnabled="false" httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</serviceBehaviors>