Setup Silverlight application with WCF service to run over HTTPS/SSL

Here are the steps in achieving this task.

1. Setup WCF service.

To do this you have to alter web.config file for the web site that hosts the service.  You have to add security setting to basisHTTP binding that is used by Silverlight application to connect to the WCF service.  Here is an example of the entire server side system.serviceModel section of web.config file

<system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WcfPortalBehavior">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                    <compression/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <bindings>
            <basicHttpBinding>
                <binding name="basicHttpBinding_IWcfPortal_HTTPS"
                        maxReceivedMessageSize="2147483647"
                        receiveTimeout="00:30:00"
                        sendTimeout="00:30:00"
                        openTimeout="00:10:00"
                        closeTimeout="00:5:00">
                    <readerQuotas
                        maxBytesPerRead="2147483647"
                        maxArrayLength="2147483647"
                        maxStringContentLength="2147483647"
                        maxDepth="1024"/>
            <security mode="Transport">
                <transport clientCredentialType="None"/>
            </security>

                </binding>
            </basicHttpBinding>
        </bindings>
        <services>
            <service name="RootNamespace.Library.Wcf.Host"
                     >

                <endpoint binding="basicHttpBinding"
                          contract="RootNamespace.Library.Wcf.Silverlight.IWcfPortal"
                          bindingConfiguration="basicHttpBinding_IWcfPortal_HTTPS">

                </endpoint>
            </service>
        </services>

    </system.serviceModel>

2.  Configure Silverlight application to use HHTPS.

To do this you have to alter ServiceReference.ClientConfig and add security setting.  Here is an example of the entire file

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding    name="basicHttpBinding_IWcfPortal"
                            maxBufferSize="2147483647"
                            maxReceivedMessageSize="2147483647"
                            receiveTimeout="00:10:00"
                            sendTimeout="00:10:00"
                            openTimeout="00:2:00"
                            closeTimeout="00:2:00">
                     <security mode="Transport" />

                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint    address="https://www.examplesite.com/WcfSilverlightPortal.svc"
                        binding="basicHttpBinding"
                        bindingConfiguration="basicHttpBinding_IWcfPortal"
                        contract="RootNamespace.Library.Wcf.Silverlight.IWcfPortal"
                        name="BasicHttpBinding_IWcfPortal" />
        </client>
    </system.serviceModel>
</configuration>

3.  Setup client access policy for the web site.

This is done by editing file at the root of your domain.  For example, if you are hosting everything in a virtual directory called Portal, your clientaccesspolicy.xml still needs to reside at the root of the domain, not in that virtual directory.  You can locate the physical path to that in properties of the root web site in IIS.  Here is an example of the entire file that enables HTTP access

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
    <cross-domain-access>
        <policy>
            <allow-from http-request-headers="*">
                <domain uri="http://*"/>
                <domain uri="https://*"/>
            </allow-from>
            <grant-to>
                <resource path="/" include-subpaths="true"/>
            </grant-to>
        </policy>
    </cross-domain-access>
</access-policy>

You can further restrict access by updating resource node or allow-from node.

You can verify the correctness of clientaccesspolicy.xml location in SilverlightSpy.  Go to Tools->Cross-domain access policy valuator an enter your domain there, for example http://www.examplesite.com

You can also hit your service directly.  If you follow the example in step 2, you can type in the following into your browser:https://www.examplesite.com/WcfSilverlightPortal.svc

Leave a Reply

Your email address will not be published. Required fields are marked *