WCF Web API 安全

标签 wcf wcf-web-api

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

最佳答案

要支持 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)));

关于WCF Web API 安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6589813/

相关文章:

WCF绕过防火墙的两种HTTP通信方式

c# - ASP.NET WebAPI 中 HttpServiceHost 的等价物是什么?

c# - 部署 webAPI 服务器端所需的最少文件

c# - 缺少基于任务的 WCF 服务的帮助说明

c# - 如何通过成员(member)/授权在 Azure 上启用 WCF 服务的流传输?

wcf服务已知类型属性问题

asp.net-mvc - MVC4 WebAPI 原因短语?

asp.net-mvc - 我可以在同一个 AppHarbor 站点上运行 MVC 应用程序和 WCF Web Api 吗?

c# - WCF Web API 和 ASP.NET Web API 有什么区别

wcf - Unity.wcf 和 InstanceContextMode.Single