Silverlight自动选择http和https之间的安全传输模式

时间:2011-01-21 10:03:10

标签: silverlight wcf ssl silverlight-4.0

我们的Silverlight应用程序可以在 http https (SSL,使用传输安全性)模式下运行。在我们的ServiceReferences.ClientConfig文件中,我们只是以这种方式配置了我们的服务端点:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="DefaultEndpoint"
                 maxBufferSize="2147483647"
                 maxReceivedMessageSize="2147483647">
          <security mode="None" />
          <!-- Enable for SSL: mode="Transport" -->
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="/services/DefaultService.svc"
                binding="basicHttpBinding"
                bindingConfiguration="DefaultEndpoint"
                contract="OurNamespace.IOurContractAsync"
                name="DefaultEndpoint" />
    </client>
  </system.serviceModel>
</configuration>

可以在两种模式下访问配置的端点。它只取决于加载XAP文件的上下文:来自http://example.com/slpage.htmlhttps://example.com/slpage.html。不幸的是,我们必须在“无”和“传输”之间手动切换安全模式设置。其他一切都已按预期工作。当安全模式为“无”并且我们通过https访问时,我们得到一个例外“提供了..https但是预期了http ......”,反之亦然。是否有机会让Silverlight自动决定应该使用哪种安全模式?解决这个问题的最简单方法是什么?

提前致谢

托马斯

2 个答案:

答案 0 :(得分:5)

我们最终得到了以下解决方案(不完全是Valentin所建议的,但+1求救!):

ServiceReferences.ClientConfig包含绑定和端点配置,如下所示:

<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="DefaultBinding"
                 maxBufferSize="2147483647"
                 maxReceivedMessageSize="2147483647">
          <security mode="None" />
        </binding>
      </basicHttpBinding>
      <customBinding>
        <binding name="SecureBinding">
          <textMessageEncoding messageVersion="Soap12WSAddressing10" />
          <httpsTransport maxBufferSize="2147483647"
                          maxReceivedMessageSize="2147483647" />
        </binding>
      </customBinding>
    </bindings>
    <client>
      <endpoint address="/services/DefaultService.svc"
                binding="basicHttpBinding"
                bindingConfiguration="DefaultBinding"
                contract="OurNamespace.IOurContractAsync"
                name="DefaultEndpoint" />
      <endpoint address="/services/DefaultService.svc"
                binding="customBinding"
                bindingConfiguration="SecureBinding"
                contract="OurNamespace.IOurContractAsync"
                name="SecureEndpoint" />
    </client>
  </system.serviceModel>
</configuration>

在初始化时,我们读取App.Current.Host.Source.Scheme属性。服务客户端由ChannelFactory生成,代码类似于此代码段:

protected string EndpointName {
  get {
    return (App.Current.Host.Source.Scheme == "https") ?
      "SecureEndpoint" : "DefaultEndpoint";
  }
}

protected IOurContractAsync CreateInterface() {
  var channelFactory = ChannelFactory<IOurContractAsync>(EndpointName);
  return channelFactory.CreateChannel();
}

希望这有帮助!

祝你好运, 托马斯

答案 1 :(得分:1)

我认为可以通过多种方式从您的网页向SL app提供initparams,例如

  

param name =“initParams”   value =“Https = true”

表示https页面  对于html页面为false。解析它  在SL内部并设置安全模式  端点。

您可以在SL应用程序中以编程方式创建/编辑端点代理。

另一种方法可能是设置基于SL app内部链接的传输行为而不使用initparams(如果以https开头 - >运输其他无)我相信一旦下载了sl app链接应该不是相对的,这应该是一个有效的解决方案。

您可以创建一个工厂方法来创建服务代理并将此安装代理逻辑放在其中,这比完全删除此serviceconfig文件更简单。

你只需致电

MyServiceClient client = Factory.MakeClient() 

我认为这是一个优雅的解决方案。在MakeClient中,您可以决定使用哪种传输安全性及其完成。