【问题标题】:Silverlight automatically choose security transport mode between http and httpsSilverlight 在 http 和 https 之间自动选择安全传输模式
【发布时间】:2011-06-13 01:11:14
【问题描述】:

我们的 Silverlight 应用程序可以在 httphttps(SSL,使用 Transport 安全性)模式下运行。在我们的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 is provided but http is expected...”,反之亦然。有没有机会让 Silverlight 自动决定应该使用哪种安全模式?解决这个问题最简单的方法是什么?

提前致谢

托马斯

【问题讨论】:

    标签: silverlight wcf ssl silverlight-4.0


    【解决方案1】:

    我们最终得到了以下解决方案(不完全是 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 属性。 Service客户端由ChannelFactory生成,代码类似这个sn-p:

    protected string EndpointName {
      get {
        return (App.Current.Host.Source.Scheme == "https") ?
          "SecureEndpoint" : "DefaultEndpoint";
      }
    }
    
    protected IOurContractAsync CreateInterface() {
      var channelFactory = ChannelFactory<IOurContractAsync>(EndpointName);
      return channelFactory.CreateChannel();
    }
    

    希望这会有所帮助!

    最好的问候, 托马斯

    【讨论】:

    • 是的,如果您可以从 Scheme 中毫无问题地读取它,那么看起来比传递 https=true 参数要好。
    【解决方案2】:

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

    参数名称="initParams" 值="Https=true"

    对于 https 页面 html页面为false。解析它 在 SL 中设置安全模式 端点。

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

    另一种方法可能是在没有 initparams 的情况下基于 SL 应用程序内的链接设置传输行为(如果以 https 开头 ->transport 否则没有)我相信一旦下载了 sl 应用程序,链接应该不是相对的,这应该是一个可行的解决方案。

    你可以制作一个工厂方法来创建服务代理,并将这个设置代理逻辑放在里面,这比完全删除这个服务配置文件要简单。

    你只需调用

    MyServiceClient client = Factory.MakeClient() 
    

    我认为这是一个足够优雅的解决方案。在 MakeClient 中,您决定使用什么传输安全性并完成。

    【讨论】:

    • 感谢您的提示。是的,我已经知道一些程序化解决方案,我可以完全抛弃ServiceReferences.ClientConfig,我不喜欢这样做......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 2011-08-16
    • 2015-02-18
    • 1970-01-01
    • 2011-12-22
    • 2018-10-05
    相关资源
    最近更新 更多