How to configure WCF client binding when using X509 certificate? - c#

I am trying to set the credentials in my WCF client as shown below.
ClientCredentials loginCredentials = new ClientCredentials();
loginCredentials.UserName.UserName = this.UserId;
loginCredentials.UserName.Password = this.Password;
loginCredentials.ClientCertificate.Certificate = new X509Certificate2(this.Certificate);
var defaultCredentials = channelFactory.Endpoint.Behaviors.Find<ClientCredentials>();
channelFactory.Endpoint.Behaviors.Remove(defaultCredentials);
channelFactory.Endpoint.Behaviors.Add(loginCredentials);
I have the wcf client binding setup as below.
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="PPWSApiOrderBinding" 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="Transport">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://ws.test.globalgateway.com:443/wsapi/services"
binding="basicHttpBinding" bindingConfiguration="PPWSApiOrderBinding"
contract="PPWebService.PPWSApiOrder" name="PPWSApiOrderPort" />
</client>
</system.serviceModel>
I am receiving an error "The http request is unauthorized with client authentication scheme 'anonymous'. The authentication header received from the server was 'Basic realm=..."
The remote server returned an error: (401) Unauthorized.
Any help is appreciated. I am not sure what needs to be changed in the binding section of my app.config file. Thanks!

looks like your error could be in the Transport node
look at this MSDN page for the reference
here is an example.
<wsHttpBinding>
<binding name="TransportSecurity">
<security mode="Transport" />
<transport clientCredentialType = "Windows" />
</security>
</binding>
</wsHttpBinding >
or
<wsHttpBinding>
<binding name="MessageSecurity">
<security mode="Message" />
<message clientCredentialType = "Certificate" />
</security>
</binding>
</wsHttpBinding >
HttpBinding MSDN

Related

The provided URI scheme 'http' is invalid; expected 'net.tcp'. parameter name: via

Hi i am having an issue with a wpf app connecting to a wcf service. the service has a netTcpBinding and uses an http scheme and and if i try to connect to it using the netTcpBinding i get the error
The provided URI scheme 'http' is invalid; expected 'net.tcp'. parameter name: via
Here is the web config
<netTcpBinding>
<binding name="netTcpBindingConfig" closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="false" transferMode="Buffered"
transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10"
maxBufferPoolSize="524288"
maxBufferSize="65536" maxConnections="10"
maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192"
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows"
protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
end point
<endpoint address="http://FinanceIccWS_5_0/IccService.svc"
binding="netTcpBinding" bindingConfiguration="netTcpBindingConfig"
contract="Gatherers.ICCService.ICCService" name="NetTcpBinding_ICCService" >
<identity>
<userPrincipalName value="cssqaaspsvc#corp.fmglobal.com" />
</identity>
and code theres is a catch method afterwards but didn't see it being relavent
string address = "http://FinanceIccWS_5_0/IccService.svc?wsdl";
EndpointAddress endpointAddress = new EndpointAddress(address);
NetTcpBinding binding = new NetTcpBinding("netTcpBindingConfig");
try
{
var proxy = new CCServiceClient(binding,endpointAddress);
proxy.Open();
test.TestDetails = proxy.State.ToString();
test.TestPassed = true;
report.RetrievalStatus = RetrievalStatus.Succeeded;
report.ApplicationStatus = ApplicationStatus.Passed;
}
tried using protocol mapping but didn't seem to do anything
<protocolMapping>
<add scheme="http" binding="netTcpBinding" bindingConfiguration="netTcpBindingConfig"/>
</protocolMapping>

WCF Error Zero Application Endpoints when using Remote Client

I have a published WCF Service running correctly and verified by pulling up the wsdl? screen.
However, when I try to access any methods using code from a separate website I get the following error:
Service 'wcfServiceFromCaps.AgentWebAppServiceClient' has zero application(non- infrastructure) endpoints. This might be because no configuration file was found for your application
[ServiceContract]
interface IAgentWebAppService
{
[OperationContract]
[FaultContract(typeof(ServiceData))]
void DoWork();
[OperationContract]
[FaultContract(typeof(ServiceData))]
String GetAgentPicURLUsingStation(string station);
}
Client WebConfig
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IAgentWebAppService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<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" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://myfulladdresshere:87/AgentWebAppService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IAgentWebAppService"
contract="IAgentWebAppService" name="WSHttpBinding_IAgentWebAppService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
Client CS
AgentWebAppServiceClient newWcf = new AgentWebAppServiceClient();
try
{
string newstring = newWcf.GetAgentPicURLUsingStation(this.Phone);
newpicurl = newstring;
}
catch (FaultException<wcfServiceFromCaps.ServiceData> Fex)
{
....
}
What am I missing?
Update!**
I resolved it by cleaning everything up and recreating from scratch.
Now I'm getting the following message:
Cannot open Database "dbname" requested by the login. The login
failed. Login failed for user "Servername$"

How do i call webservice from windows service

I have a windows service from which I am trying to call a method under remote webservice but I am getting error that "Could not find default endpoint element that references contract 'MyWebService.BookingCitySoap' 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 contract could be found in the client element."
Following is the code for that
.cs File
MyWebService.BookingCitySoapClient ws = new MyWebService.BookingCitySoapClient();
ws.CallBookStatus();
ws.CallCanStatus();
App.config
<bindings>
<basicHttpBinding>
<binding name="BookingCitySoap" 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:51317/Web/BookingCity.asmx"
binding="basicHttpBinding" bindingConfiguration="BookingCitySoap"
contract="MyWebService.BookingCitySoap" name="BookingCitySoap" />
</client>

Content type mismatch when consuming java wsdl from .net

There is a wsdl hosted on apache or jboss on a remote server that I am trying to use in my c# project. I have added a service reference in visual studio, which automatically generated an app.config file for me, where the system.serviceModel section looks as follows
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ArtesiaWebServicesHttpBinding" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="655360" maxBufferPoolSize="524288" maxReceivedMessageSize="655360"
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://serverpath/ArtesiaWebServices"
binding="basicHttpBinding" bindingConfiguration="ArtesiaWebServicesHttpBinding"
contract="DAMService.ArtesiaWebServicesInterface" name="ArtesiaWebServicesHttpPort" />
</client>
</system.serviceModel>
At runtime, during a method call, I get the following error:
The content type multipart/related; type="application/xop+xml"; start=""; start-info="text/xml"; boundary="----=_Part_386_1206794365.1374255761229" of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly.
What could be the problem?
The problem was solved by changing messageEncoding property of the binding to "Mtom" instead of "Text"

WCF Service with large parameters

I've looked at a number of similar topics on SO, but haven't found one that helps with this.
Have a WCF service that takes in XML to process. The XML file I'm reading from is ~600K.
The call works for small xml files (most of the time), but on the larger files I get the error:
System.ServiceModel.Security.MessageSecurityException:
An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.
where the inner exception is:
System.ServiceModel.FaultException:
The message could not be processed. This is most likely because the action 'http://tempuri.org/ISapListener/ProcessSapRoles' is incorrect or because the message contains an invalid or expired security context token or because there is a mismatch between bindings. The security context token would be invalid if the service aborted the channel due to inactivity. To prevent the service from aborting idle sessions prematurely increase the Receive timeout on the service endpoint's binding.
Like I say... it works for small files, and my open, send, receive, close, and inactivity timeouts are all set to 10 minutes. It fails in about 20-30 seconds.
Also, the clock on the server and client are perfectly in sync (I've seen that posted as an answer).
My config files as they currently stand (I've played with a lot of settings):
Server:
<bindings>
<wsHttpBinding>
<binding name="wsHttpBinding_Custom" closeTimeout="00:00:10"
openTimeout="00:01:00" receiveTimeout="00:10:00"
sendTimeout="00:10:00" bypassProxyOnLocal="false"
transactionFlow="false" hostNameComparisonMode="StrongWildcard"
messageEncoding="Text" textEncoding="utf-8"
useDefaultWebProxy="true" allowCookies="false"
maxReceivedMessageSize="1024768"
maxBufferPoolSize="1024768" >
<readerQuotas maxDepth="32" maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<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" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="CSA.GS3.Services.SapListenerBehavior"
name="CSA.GS3.Services.SapListener">
<endpoint address="" binding="wsHttpBinding"
bindingConfiguration="wsHttpBinding_Custom"
contract="CSA.GS3.Services.ISapListener">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CSA.GS3.Services.SapListenerBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
Client:
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ISapListener1"
closeTimeout="00:10:00" openTimeout="00:10:00"
receiveTimeout="00:10:00" sendTimeout="00:10:00"
bypassProxyOnLocal="false" transactionFlow="false"
hostNameComparisonMode="StrongWildcard"
messageEncoding="Text" textEncoding="utf-8"
useDefaultWebProxy="true" allowCookies="false"
maxBufferPoolSize="1024768"
maxReceivedMessageSize="1024768">
<readerQuotas maxDepth="32" maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<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" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://gs3-test.us.tycoelectronics.com/SapListener/SapListener.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ISapListener1"
contract="Gs3TestSapListener.ISapListener"
name="WSHttpBinding_ISapListener1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
I do have tracing enabled on the service, but I can't make sense out of the log files.
Other exceptions I've received while playing with the config settings:
System.ServiceModel.Security.SecurityNegotiationException
Secure channel cannot be opened because security negotiation with the remote endpoint has failed.
and
System.ServiceModel.ServiceActivationException
The requested service, 'http://../SapListener.svc' could not be activated.
If you think being able to make sense out of log files will help here, use svcTraceViewer. Just make sure you've actually set up tracing correctly. I have an article on my blog about this.
svcTraveViewer Debugging WCF Services
As regards large payloads, you may want to take a look at this MSDN article.
http://msdn.microsoft.com/en-us/library/ms733742.aspx
In particular Streaming Data.
System.ServiceModel.ServiceActivationException
The requested service, 'http://../SapListener.svc' could not be activated.
This could be a compilation error ou invalid configuration.
Is it WCF 4.0? Then you could remove your custom configuration and use automatic bindings. I also suggest you to try a binding other than wsHttpBinding, like basicHttpBinding.
I was able to get this to work with the following configuration:
Server:
<bindings>
<wsHttpBinding>
<binding name="wsHttpBinding_Custom"
closeTimeout="00:10:00"
openTimeout="00:10:00"
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
maxReceivedMessageSize="2097152"
bypassProxyOnLocal="false" transactionFlow="false"
hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2097152" messageEncoding="Text"
textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="32" maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<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" />
</security>
</binding>
</wsHttpBinding>
</bindings>
Client:
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ISapListener1" closeTimeout="00:10:00"
openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
bypassProxyOnLocal="false" transactionFlow="false"
hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2097152"
maxReceivedMessageSize="2097152"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<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" />
</security>
</binding>
</wsHttpBinding>
</bindings>
The only difference I can see is that I bumped up the maxReceivedMessageSize and maxBufferPoolSize... maybe I'm missing something else? If that was the issue, then the problem was that my overhead for the call was adding an additional 400+K to the 600K xml data I was sending
If possible and acceptable to client, you can break down the file into smaller chunks and send across, provided you don't have digital certificate etc and sorting options at client side.

Categories