如何为 HTTPS 传输配置 wcf Web api 服务?有谁知道这在最终版本中会有多大变化,因为这是他们说会改变的领域之一?
问问题
1326 次
3 回答
6
要支持 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)));
于 2011-07-06T08:32:40.987 回答
5
在我们的最新版本中,您可以使用 HttpConfiguration 对象设置绑定,而无需创建新主机。它公开了一个SetSecurity方法,您可以设置它来更改安全模式。
于 2011-09-02T15:48:15.310 回答
2
这是我在 Global.asax 中的配置,我检查了 URI,然后使用了正确的模式。在 IIS 和 IIS Express 中运行良好。. . . 我的目标是基本的 HTTPS,但是 IIS express 将 HTTP URI 保持在“绑定”中,除非你处理它,否则你会陷入无限循环(http://screencast.com/t/kHvM49dl6tP,http://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))
};
于 2011-11-28T19:17:38.343 回答