【问题标题】:WCF Web API securityWCF Web API 安全性
【发布时间】:2011-11-29 03:17:38
【问题描述】:

如何为 HTTPS 传输配置 wcf web api 服务? 有谁知道这在最终版本中会有多大变化,因为这是他们说会改变的领域之一?

【问题讨论】:

    标签: wcf wcf-web-api


    【解决方案1】:

    要支持 HTTPS,您需要在 HttpBinding 上启用传输安全性。这可以通过从 HttpConfigurableServiceHostFactory 派生并像这样覆盖 CreateServiceHost 来完成:

    public class HypertextTransferProtocolSecureServiceHostFactory : HttpConfigurableServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            var configurationBuilder = HttpHostConfiguration.Create();
    
            var host = new HttpConfigurableServiceHost(serviceType, configurationBuilder, baseAddresses);
    
            foreach (var endpoint in host.Description.Endpoints.Where(e => e.ListenUri.Scheme == "https"))
            {
                var binding = endpoint.Binding as HttpBinding;
    
                if (binding != null)
                {
                    binding.Security.Mode = HttpBindingSecurityMode.Transport;
                }
            }
            return host;
        }
    }
    

    最后必须将 HypertextTransferProtocolSecureServiceHostFactory 添加到 RouteTable:

    RouteTable.Routes.Add(new ServiceRoute("routePrefix", new HypertextTransferProtocolSecureServiceHostFactory(), typeof(ServiceType)));
    

    【讨论】:

    • 非常感谢 hskan:>}
    【解决方案2】:

    在我们的最新版本中,您可以使用 HttpConfiguration 对象设置绑定,而无需创建新主机。它公开了一个SetSecurity 方法,您可以设置它来更改安全模式。

    【讨论】:

    • 格伦在某处有这样的例子吗?我正在努力重新配置 Web api 服务以通过 https 工作。
    【解决方案3】:

    这是我在 Global.asax 中的配置,我检查了 URI,然后使用了正确的模式。在 IIS 和 IIS Express 中运行良好。 . . .我的目标是 Basic over HTTPS,但是 IIS express 将 HTTP URI 保留在“绑定”中,除非你处理它,否则你会陷入无限循环(http://screencast.com/t/kHvM49dl6tPhttp://screencast.com/t/5usIEy5jgPdX

                    var config = new HttpConfiguration
                           {
                               EnableTestClient = true,
                               IncludeExceptionDetail = true,
                               EnableHelpPage = true,
                               Security = (uri, binding) =>
                                              {
                                                  if (uri.Scheme.Equals("https", StringComparison.InvariantCultureIgnoreCase)) 
                                                      binding.Mode = HttpBindingSecurityMode.Transport;
                                                  else 
                                                      binding.Mode = HttpBindingSecurityMode.TransportCredentialOnly;
    
                                                  binding.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
                                              },
                               CreateInstance = ((t, i, h) => container.Resolve(t))
                           };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      相关资源
      最近更新 更多