WCF configuration to HTTPS with SSL not working - wcf

I am trying to use an HTTPS service using WCF but it keeps returning all kinds of errors. When I use the basicHttpBinding to call the HTTP URL it works fine but when I swich to webHttpBinding or wsHttpBinding and call the HTTPS URL I'm getting these errors:
The content type text/xml;charset=utf-8 of the response message does not match the content
type of the binding (application/soap+xml; charset=utf-8). If using a custom encoder,
be sure that the IsContentTypeSupported method is implemented properly.
And here is my configuration:
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehavior" name="MentorGraphicsIM">
<endpoint name="MentorGraphicsServiceEndPoint"
address=""
binding="webHttpBinding"
bindingConfiguration="webHttpPadsTouch"
behaviorConfiguration="ServiceEndPointBehavior"
contract="IMentorGraphics">
</endpoint>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding" />
</basicHttpBinding>
<webHttpBinding>
<binding name="webHttpPadsTouch">
</binding>
</webHttpBinding>
<wsHttpBinding>
<binding name="WsHttpBinding">
<security mode="Transport" />
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<!--Required default endpoint behavior when using webHttpBinding-->
<endpointBehaviors>
<behavior name="ServiceEndPointBehavior">
<webHttp />
</behavior>
<behavior name="ClientEndPointBehavior">
<webHttp defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Xml" helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint name="PadsTouch_HTTP"
address="https://XXX"
binding="wsHttpBinding"
bindingConfiguration="WsHttpBinding"
contract="IEntitle" />
</client>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
My Interface is:
[ServiceContract(Namespace = "xxx", ProtectionLevel = ProtectionLevel.None)]
public interface IMentorGraphics
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "MyMethod", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
ServiceResponse MyMethod(MyParams #param);
}

replace service binding from bindingConfiguration="webHttpPadsTouch" to bindingConfiguration="WsHttpBinding"

Related

Creating REST and SOAP endpoints for a WCF service?

I need a service with two endpoints.When I create service with 1 endpoint point, it works fine but when I add 2 endpoints it complains endpoint not found.
Endpoint without address extension works fine but not with address ('download')
Here is my configuration:
Server
<serviceBehaviors>
<behavior name="MessageStreamBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<!--stream for video upload-->
<binding name="httpLargeMessageStream"
maxReceivedMessageSize="4294967295"
openTimeout="00:01:00" closeTimeout="00:30:00" sendTimeout="00:30:00"
transferMode="Streamed">
<security mode="Transport" />
</binding>
</basicHttpBinding>
<webHttpBinding>
<!--stream for video download-->
<binding name="webHttpLargeMessageStream"
maxReceivedMessageSize="4294967295"
openTimeout="00:01:00" closeTimeout="00:30:00" sendTimeout="00:30:00"
transferMode="Streamed">
<security mode="Transport" />
</binding>
</webHttpBinding>
</bindings>
<services>
<!--service for streamservice; video upload and download-->
<service name="Trigger.CSSD.Service.Services.MediaService" behaviorConfiguration="MessageStreamBehavior" >
<endpoint name="restDownloadHttpStream"
behaviorConfiguration="webHttpBehavior" address="download"
binding="webHttpBinding" bindingConfiguration="webHttpLargeMessageStream"
contract="Trigger.CSSD.Service.Services.IDownloadMediaService" />
<endpoint name="uploadHttpStream"
binding="basicHttpBinding" bindingConfiguration="httpLargeMessageStream"
contract="Trigger.CSSD.Service.Services.IUploadMediaService" />
</service>
</services>
Client
<endpoint address="https://cssd-services:44300/MediaService.svc"
binding="basicHttpBinding" bindingConfiguration="basicHttpStream"
contract="MediaService.IUploadMediaService" name="basicHttpStream" />
<endpoint address="https://cssd-services:44300/MediaService.svc/download"
binding="webHttpBinding" bindingConfiguration="webHttpStream" behaviorConfiguration="webHttpBehavior"
contract="MediaService.IDownloadMediaService" name="webHttpStream" />
</client>
SERVICE
[ServiceContract]
public interface IDownloadMediaService
{
[OperationContract]
// [ContentType("video/mp4")]
// [WebGet(UriTemplate = "media/{name}")]
Stream GetMedia(String name);
}
[ServiceContract]
public interface IUploadMediaService
{
//http://msdn.microsoft.com/en-us/library/ms751463.aspx
[OperationContract]
FileUploadResponseMessage UploadFile(FileUploadMessage message);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class MediaService : IDownloadMediaService, IUploadMediaService
{......
}
ANy suggestion? Thanks.

Not able to access the methods from secure wcf rest service

I have a running wcf rest service hosted on IIS. I have successfully added the .x509 certificates to the client and the server and it works fine.As it is a secure service it is accessed using https The problem I am facing is that I am not able to access any of the methods from the service it keeps throwing 404 error however when I browse the service from IIS using https link it open the WSDL fine but throws 404 error while accessing any of the methods.
Here is my Interface implementation of a method from the service.
[ServiceContract]
public interface IRestDemo
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/DoWork/{name}")]
string DoWork(string name);
}
Here is my configuration file:
<system.serviceModel>
<services>
<service name="RestDemo.RestDemo" behaviorConfiguration="serviceBehavior">
<host>
<baseAddresses>
<add baseAddress="https://localhost/RestDemo/RestDemo.svc" />
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" contract="RestDemo.IRestDemo" behaviorConfiguration="web">
<!--<identity>
<dns value="localhost"/>
</identity>-->
</endpoint>
<endpoint address="mex" binding="mexHttpsBinding" contract="RestDemo.IRestDemo" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="web" crossDomainScriptAccessEnabled="true">
<security mode="Transport">
<transport clientCredentialType="Certificate" />
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<!--<serviceCredentials>
<clientCertificate>
<authentication certificateValidationMode="PeerTrust"
trustedStoreLocation="LocalMachine" />
</clientCertificate>
<serviceCertificate findValue="ServerCertificate"
storeLocation="LocalMachine"
storeName="My"
x509FindType="FindBySubjectName" />
</serviceCredentials>-->
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" httpsGetUrl="/RestDemo.svc" />
<!-- 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>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint
crossDomainScriptAccessEnabled="true">
</standardEndpoint>
</webScriptEndpoint>
</standardEndpoints>
</system.serviceModel>
Any suggestions would be really appreciated.

DotNetOpenAuth and ResourceServer service https configuration

I am trying to configure the DataApi.svc service of the DotNetOpenAuth to call my resources via https using AJAX.
I can call the service and hit the code behind but the OperationContext.Current.ServiceSecurityContext will be not authenticated
In IIS, I have "Anonymous authentication" set to "true".
In Fiddler I can see that the header is sent:
Authorization: Bearer gAAAAMcRmG5vw3LykShq7cNOEGUACBiNtlVGxGYdSVfkkXjR-[truncated]
The interface is decorated like that:
[ServiceContract]
public interface IDataApi {
[OperationContract, WebGet(UriTemplate = "/email", ResponseFormat = WebMessageFormat.Json)]
string GetEmail();
And here is my config:
<bindings>
<wsHttpBinding>
<binding>
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</wsHttpBinding>
<webHttpBinding>
<binding>
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="DataApiBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpsGetEnabled="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="DataApiWebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="DataApiBehavior" name="OAuthResourceServer.DataApi">
<endpoint address="" binding="wsHttpBinding" contract="OAuthResourceServer.Code.IDataApi" />
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
<endpoint address="web" binding="webHttpBinding" contract="OAuthResourceServer.Code.IDataApi" behaviorConfiguration="DataApiWebBehavior">
</endpoint>
</service>
</services>
Any idea of what can be wrong?
Thanks!
I was missing the
<serviceAuthorizationserviceAuthorizationManagerType="OAuthResourceServer.Code.OAuthAuthorizationManager, OAuthResourceServer" principalPermissionMode="Custom" />
in the service behavior! Solved :)

WCF Multiple Endpoints is not working

I am trying to let my WCF service to have operations that can be called through a proxy client and through REST call, i am using the following configurations:
<services>
<service behaviorConfiguration="SecureBehavior" name="Payment">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="secureWS" contract="IPayment"/>
<endpoint address="rest" binding="webHttpBinding" behaviorConfiguration="webBehavior" contract="IPayment"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<bindings>
<mexHttpBinding>
<binding name="userMex"/>
</mexHttpBinding>
<wsHttpBinding>
<binding name="secureWS">
<security mode="Message">
<message clientCredentialType="Windows" negotiateServiceCredential="true" establishSecurityContext="true"/>
</security>
</binding>
<binding name="publicWS">
<security mode="None"/>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="SecureBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceCredentials>
<windowsAuthentication allowAnonymousLogons="false"/>
</serviceCredentials>
</behavior>
<behavior name="PublicBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceCredentials>
<windowsAuthentication allowAnonymousLogons="true"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</services>
and here is my code:
[ServiceContract]
public interface IPayment
{
[OperationContract]
PaymentResult Finalize(string TransactionID, string CertificatePath);
[OperationContract]
[WebGet(UriTemplate = "rest")]
System.IO.Stream GetPayment();
}
Now whenever i run my service i receive this error:
Operation 'Finalize' of contract 'IPayment' specifies multiple request body parameters to be serialized without any wrapper elements. At most one body parameter can be serialized without wrapper elements. Either remove the extra body parameters or set the BodyStyle property on the WebGetAttribute/WebInvokeAttribute to Wrapped.
Where here i would like to keep Finalize operation to be only called via .NET client and the GetPayment operation to be called through any browser.
If you do not want your Finalize method to be called from a client connecting via the webhttp endpoint and you don't want GetPayments to be called from a client connecting via wshttp you can simply split your contract in two.
Assuming you are hosting in IIS you might need to do a little trick to make sure that it works. Let me give you an example using the details from the question...
First of all here is the code for the two services...
[ServiceContract]
public interface IPayment
{
[OperationContract]
[WebGet(UriTemplate = "rest")]
System.IO.Stream GetPayment();
}
[DataContract]
public class PaymentResult
{
}
[ServiceContract]
public interface IMakePayment
{
[OperationContract]
PaymentResult Finalize(string TransactionID, string CertificatePath);
}
// Maybe you really should have the two services separate but if you do
// want to implement them both in a single class you can do this
public abstract class PaymentBase : IMakePayment, IPayment
{
// ... Implement both interfaces here
public PaymentResult Finalize(string TransactionID, string CertificatePath)
{
return null;
}
public System.IO.Stream GetPayment()
{
return null;
}
}
public class MakePayment : PaymentBase
{
// Empty
}
public class Payment : PaymentBase
{
// Empty
}
Now create two .svc files like so:
MakePayment.svc
<%# ServiceHost Language="C#" Debug="true" Service="WebApplication1.MakePayment" %>
Payment.svc
<%# ServiceHost Language="C#" Debug="true" Service="WebApplication1.Payment" %>
And finally here is the system.servicemodel configuration:
<system.serviceModel>
<services>
<service behaviorConfiguration="SecureBehavior" name="WebApplication1.MakePayment">
<endpoint binding="wsHttpBinding" bindingConfiguration="secureWS" contract="WebApplication1.IMakePayment"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" bindingConfiguration="userMex"/>
</service>
<service behaviorConfiguration="PublicBehavior" name="WebApplication1.Payment">
<endpoint binding="webHttpBinding" behaviorConfiguration="webBehavior" bindingConfiguration="publicWS" contract="WebApplication1.IPayment"/>
</service>
</services>
<bindings>
<mexHttpBinding>
<binding name="userMex"/>
</mexHttpBinding>
<wsHttpBinding>
<binding name="secureWS">
<security mode="Message">
<message clientCredentialType="Windows" negotiateServiceCredential="true" establishSecurityContext="true"/>
</security>
</binding>
</wsHttpBinding>
<webHttpBinding>
<binding name="publicWS">
<security mode="None"/>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="SecureBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceCredentials>
<windowsAuthentication allowAnonymousLogons="false"/>
</serviceCredentials>
</behavior>
<behavior name="PublicBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceCredentials>
<windowsAuthentication allowAnonymousLogons="true"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
There you go - if you connect to /MakePayment.svc you will connect via WSHTTP and be able to call FinalizePayment and if you go to /Payments.svc/rest it will call the GetPayment method where you are returning a stream.

How to use SSL with a WCF web service?

I have a web service in asp.net running and everything works fine. Now I need to access some methods in that web-service using SSL. It works perfect when I contact the web-service using http:// but with https:// I get "There was no endpoint listening at https://...".
Can you please help me on how to set up my web.config to support both http and https access to my web service. I have tried to follow guidelines but I can't get it working.
Some code:
My TestService.svc:
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class TestService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public bool validUser(string email) {
return true;
}
}
My Web.config:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<behaviors>
<endpointBehaviors>
<behavior name="ServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehavior" name="TestService">
<endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior"
binding="webHttpBinding" bindingConfiguration="ServiceBinding"
contract="TestService" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="ServiceBinding" maxBufferPoolSize="1000000" maxReceivedMessageSize="1000000">
<readerQuotas maxDepth="1000000" maxStringContentLength="1000000" maxArrayLength="1000000" maxBytesPerRead="1000000" maxNameTableCharCount="1000000"/>
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
Instead of using the webHttpBinding, try creating a customBinding instead:
<customBinding>
<binding name="poxBindingHttps" closeTimeout="00:00:20">
<textMessageEncoding writeEncoding="utf-8" />
<httpsTransport manualAddressing="true"/>
</binding>
<binding name="poxBindingHttp" closeTimeout="00:00:20">
<textMessageEncoding writeEncoding="utf-8" />
<httpTransport manualAddressing="true"/>
</binding>
</customBinding>
You'll also need to setup the webHttpBehavior like so:
<behaviors>
<endpointBehaviors>
<behavior name="ajaxBehavior">
<enableWebScript/>
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
And finally, your service & endpoint:
<services>
<service name="TestService">
<endpoint name="sslDefault" address="" binding="customBinding" bindingConfiguration="poxBindingHttps" behaviorConfiguration="ajaxBehavior" contract="TestService"/>
<endpoint name="noSslDefault" address="" binding="customBinding" bindingConfiguration="poxBindingHttp" behaviorConfiguration="ajaxBehavior" contract="TestService"/>
</service>
</services>
Hopefully that works out for you in creating an SSL endpoint
It sounds like there is more an issue with IIS no listening on port 443 (The port used for HTTPS). Check IIS that there is a certificate installed and that it is listening on the secure port.