WCF Windows Authentication Error - c#

I'm trying to consume a Service with an WCF Console Application. But the only thing I get as a Response is "The remote server returned an unexpected response: (400) Bad Request."
The Service is configured with the following options:
<services>
<service name="TryOut.BasicService" behaviorConfiguration="NicksBasicBehavior">
<host>
<baseAddresses>
<add baseAddress="http://192.168.10.67:8000/Service" />
</baseAddresses>
</host>
<endpoint address ="http://192.168.10.67:8000/Service"
name="NicksEndpoint"
binding="basicHttpBinding"
contract="TryOut.IBasicService"
bindingConfiguration="BasicBinding"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="NicksBasicBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<binding name="BasicBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows"/>
</security>
</binding>
The configuration file of my Client is the following:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="NicksEndpoint" 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" />
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://192.168.10.67:8000/Service" binding="basicHttpBinding"
bindingConfiguration="NicksEndpoint" contract="ServiceReference1.IBasicService"
name="NicksEndpoint" />
</client>
Now everything works fine when I'm starting the client on the same PC as the service. But if I'm try to running the Client on a machine which is not in the domain of the server PC then I get an error, although I provide the Login information's in the client code:
c.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential("Nick", "password", "mydomain");
Does anybody got an idea what I'm doing wrong and how can I fix it? Thanking you in anticipation!

According to this article on MSDN, it looks as if you need to specify your credentials differently:
WCFTestService.Service myService = new WCFTestService.Service();
myService.Credentials = System.Net.CredentialCache.DefaultCredentials;
MessageBox.Show(myService.GetData(123, true));
myService.Dispose();
Could you try to use this code and try to pass the Windows credentials into myService.Credentials ? Does that work?
Marc

have you tried specifing the userPrincipalName in the identity element on your clients endpoint?

Related

wcf service the remote server returned an error (400) bad request

I have a windows application that calls WCF service. I'm getting 400 Bad Request error in some clients.(Some clients I don't get error.) I increased maxReceivedMessageSize but it didn't work.
I increased maxReceivedMessageSize and other parameters.
Client Side app.config:
<bindings>
<wsHttpBinding>
<binding name="BasicHttpBinding_IAracRandevuWS"
closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2000000" maxReceivedMessageSize="2000000"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="2000000" maxStringContentLength="2000000" maxArrayLength="2000000"
maxBytesPerRead="2000000" maxNameTableCharCount="2000000" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
WCF Service app.config:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="BasicHttpBinding_IAracRandevuWS"
openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
allowCookies="false" bypassProxyOnLocal="false" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8"
useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceSecurityAudit auditLogLocation="Application" serviceAuthorizationAuditLevel="Failure" messageAuthenticationAuditLevel="Failure" suppressAuditFailure="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="wsHttpBinding" scheme="http"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
How does the client generate the configuration file? Which way you use to call the service, adding service reference or ChannelFactory?
The wshttpbinding configuration does not well applied in the wshttpbinding, since we use the protocol mapping to host the service without using Bindingconfiguration property.
<protocolMapping>
<add binding="wsHttpBinding" scheme="http"/>
</protocolMapping>
Therefore, the default Clientcredentialtype is windows credential, and the security mode is message by default.
https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/wcf/security-of-wshttpbinding
We need to explicitly provide the windows credential on the client side.
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
client.ClientCredentials.Windows.ClientCredential.UserName = "administrator";
client.ClientCredentials.Windows.ClientCredential.Password = "abcd1234!";
In order to test this, please do not use Fiddler, and call it from another computer in Console application by using add service reference.
Feel free to let me know if the problem still exists.
It was a network issue. After update switch, the problem has been solved.

WCF Service not communicating on same server

WCF is deployed on IIS7 and access over the network AND Website(ASP.NET C#) is also deployed on same server. Over the network WCF communicate successfully but On localhost server WCF does not communicate with website.
my configuration for wcf is
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IWITSService" closeTimeout="00:00:05"
openTimeout="00:00:05" receiveTimeout="00:10:00" sendTimeout="00:00:05"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<!--<reliableSession ordered="true" inactivityTimeout="00:10:00" />-->
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:4570/WITSWCFService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IWITSService"
contract="WITS.IWITSService" name="BasicHttpBinding_IWITSService" >
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
AND Website Configuration is given below
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IWITSService" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferPoolSize="524288">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
<!--<webHttpBinding>
<binding maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" />
</webHttpBinding>-->
</bindings>
<services>
<service name="WITSService.WITSService">
<clear />
<endpoint binding="basicHttpBinding" contract="WITSService.WITSService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Are running on localhost only or localhost:4570. If you are using port 4570 you'll need to open it up on a real server.
What does the WCF endpoint return if you hit the URL in the browser, that is usually very telling of the problem.

Error when calling web service with lots of data

I have a WCF service that is hosted locally. My web.config looks like this:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_INavigationService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="6553600" maxBufferPoolSize="5242880" maxReceivedMessageSize="6553600"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/WebServices/NavigationService.svc/"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_INavigationService"
contract="NavigationService.INavigationService" name="BasicHttpBinding_INavigationService" />
</client>
</system.serviceModel>
My service works fine if I pass in less then 8K of text. Anything more and I get the following error:
Sys.WebForms.PageRequestManagerServerErrorException: The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'UpdateSiteMap'. The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 75, position 166
Can someone tell me how to increase the text quota? As you can see, I have it set to maximum integer size.
EDIT
After Tim's suggestion, here is my new Web.Config file:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="6553600" maxBufferPoolSize="5242880" maxReceivedMessageSize="6553600"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/WebServices/NavigationService.svc/"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_INavigationService"
contract="NavigationService.INavigationService" name="BasicHttpBinding_INavigationService" />
</client>
<services>
<service name="Navigation.INavigationService">
<endpoint address=""
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_INavigationService"
contract="NavigationService.INavigationService" />
</service>
</services>
Based on the error message and your description of the issue, you need to ensure that you have the same settings for your service, referencing the same defined binding in the bindingConfig attirbute of the endpoint element:
<system.serviceModel>
<services>
<service name="Navigation.INavigationService">
<endpoint address=""
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_INavigationService"
contract="NavigationService.INavigationService" />
</service>
</services>
</system.serviceModel>
If you're using .NET 4.0 and have not specified the service element, then you are using a default endpoint and default binding (which means your limited to 8192 for maxStringContentLength). The easiest way to overcome this is to remove the name attribute from the binding section of your config - .NET will then use your definition as the default binding. For example:
<binding closeTimeout="00:01:00" ....
Note no name attribute is set.
You may need to use option 1 in this case, or create a similar binding definition with no name, as I'm not 100% sure that default bindings are used for the clients; they definitely are for the service.
See A Developer's Introduction to Windows Communication Foundation 4 for more information on default bindings and endpoints.
Additional Answer Based On Edit
Try this:
<client>
<endpoint address="http://localhost/WebServices/NavigationService.svc/"
binding="basicHttpBinding"
contract="NavigationService.INavigationService"
name="BasicHttpBinding_INavigationService" />
</client>
Delete the <services> section as the default endpoint mechanism for WCF 4.0 will handle that for you, and delete the bindingConfiguration attribute from the client endpoint.

Consume WCF secured with SSL certificate in a winform application

I hosted a .svc WCF in IIS 6.0. I added a reference to that web service in a winform application. However, when I call any method of my service I get a System.Net.WebException: The request failed with HTTP status 403: Forbidden.
I have the certificate installed in my personnal store.
Here's my server config:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IAwsService" 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"/>
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
</basicHttpBinding>
<wsHttpBinding>
<binding name="TransportSecurity">
<security mode="Transport">
<transport clientCredentialType="Certificate"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://teclpo02.srr.fr:1098/AwsService/" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAwsService" contract="wsAws.IAwsService" name="BasicHttpBinding_IAwsService"/>
</client>
<services>
<service name="AuthWorkStation_Wcf_Web.AwsService" behaviorConfiguration="ServiceBehavior">
<endpoint name="" address="" binding="wsHttpBinding" bindingConfiguration="TransportSecurity" contract="AuthWorkStation_Wcf_Web.IAwsService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceCredentials>
<clientCertificate>
<authentication certificateValidationMode="None" revocationMode="NoCheck" />
</clientCertificate>
<serviceCertificate storeName="Root" findValue="CA_SRR_DISTRIB" x509FindType="FindBySubjectName" />
</serviceCredentials>
<serviceMetadata httpsGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
Do I have to edit my winform app.config file ? Do I have to write some line of code to tell where is the certificate ?
The problem is that you have transport security which requires SSL/Https. But your address is http.
Change your address to https
From the code you posted:
<client>
<endpoint address="http://teclpo02.srr.fr:1098/AwsService/" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAwsService" contract="wsAws.IAwsService" name="BasicHttpBinding_IAwsService"/>
</client>
<services>
You are also spesifying the port number that post could be blocked.

Cannot call WCF WebService from DLL

I'm trying to call a WCF Webservice, from a dll I have made, running inside our CAD Software.
I cannot get it to work though.
When I try to establish my proxy, I get the following error:
Could not find endpoint element with name 'BasicHttpBinding_IAxaptaService' and contract 'AxaptaProxy.IAxaptaService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element.
I have searched around abit, and I assume the problem is due to my DLL running inside another program.
There was some articles about copying EndPoint configuration from the app, to the service, but I didn't quite catch, what I was supposed to do.
Anyone have an idea, as to how I can make this work?
The App.Config, created by my client is this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IAxaptaService" 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" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:4726/LM/AxaptaService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAxaptaService"
contract="AxaptaProxy.IAxaptaService" name="BasicHttpBinding_IAxaptaService" />
</client>
</system.serviceModel>
</configuration>
I have tried to merge this into my web.config, on the site that hosts the web-service, as this:
<system.serviceModel>
<bindings>
<customBinding>
<binding name="GetStream.customBinding0">
<binaryMessageEncoding/>
<httpTransport/>
</binding>
</customBinding>
<basicHttpBinding>
<binding name="BasicHttpBinding_IAxaptaService" 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" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="AutoCompletionAspNetAjaxBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<services>
<service name="AutoCompletion">
<endpoint address="" behaviorConfiguration="AutoCompletionAspNetAjaxBehavior" binding="webHttpBinding" contract="AutoCompletion"/>
</service>
<service name="GetStream">
<endpoint address="" binding="customBinding" bindingConfiguration="GetStream.customBinding0" contract="GetStream"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<client>
<endpoint address="http://localhost:4726/LM/AxaptaService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAxaptaService"
contract="AxaptaProxy.IAxaptaService" name="BasicHttpBinding_IAxaptaService" />
</client>
</system.serviceModel>
There are a couple of other stuff in there already. I can remove them, if that makes it easier. I've left them in, incase they have some influence on it.
So I tested the service, from a stand-alone winform application, and it works fine.
Could it be because of the App.config? Does my config get loaded, for the .dll?
You'll need to copy the connection information from MyDll.dll.config to Web.config.
Be careful to merge configuration sections rather that simply adding the new data side-by-side, or replacing it. If there are already sections with the same name, you will probably have to combine them.
Here's an article describing the guts of the WCF portions of app.config:
http://msdn.microsoft.com/en-us/library/ms734663.aspx
The main pieces are:
<system.serviceModel>
<bindings>
<!-- various bindings go here... -->
</bindings>
<client>
<!-- endpoints go here... -->
</client>
</system.serviceModel>
You'll need to combine everything within those nodes - add the various types of endpoint elements and the binding elements to your service's web.config.
So, if you have a config that looks like this:
<system.serviceModel>
<bindings>
<someBindingType name="someBinding" />
</bindings>
<client>
<endPoint name="someEndpoint />
</client>
</system.serviceModel>
You'll need to copy over the someBindingType and endPoint elements. The whole element, including ending tags (if there are any), and child elements.
Make sure you don't duplicate system.serviceModel, bindings or client elements. If they're already there, merge into them rather than creating new elements/duplicating.
I finally got it to work!
The problem was, that the app.config does not get loaded, in my .dll project.
To fix this, I created the binding, in code, instead of through the app.config, as mentioned in this thread:
WCF Configuration without a config file
Thank you for all the help though. Merlyn, without your help, I wouldn't even have gotten this far.

Categories