我构建了一个相当简单的 WCF 服务,我将其托管在 IIS 7.5 实例上。我已经完成了保护 ssl 证书以启用 https 的必要步骤。我已经解决了所有不同的 DNS 设置,所以我现在可以在给定的 Https://URL 上从全世界访问我的 WCF。目标是:对将要向服务发送数据的大约 5 个客户端进行某种客户端/服务器身份验证。保护此服务的最佳方法是什么?此时非常简单,只有一种方法。我确信 web.config 和代码隐藏会有一些变化。示例非常感谢。
这是 Web.config
<!-- language: lang-xml -->
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service name="wcflistener.Service1">
<endpoint address=""
binding="basicHttpBinding"
bindingConfiguration="secureHttpBinding"
contract="wcflistener.IService1"/>
<endpoint address="mex"
binding="mexHttpsBinding"
contract="IMetadataExchange" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="secureHttpBinding">
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
还有非常简单的 Service1.svc.cs
[DataContract]
public class Service1 : IService1
{
public void SampleMethod(DataTable table, string name)
{
//sample method logic here
}
}
最佳答案
对于 WCF,您可以使用多种安全选项,它们各有利弊:
- SSL + 使用 Windows 身份验证。 (这对于 Internet 托管服务来说通常很困难,因为每个人都需要与同一个域 Controller 对话)
- SSL + 用户名/密码:WCF 可以轻松实现这一点,客户端传入用户名/密码,服务可以使用预先配置的值验证值并允许客户端进一步。
- 基于证书的身份验证:通常,可以为客户端提供服务器证书的公钥,以便他们可以使用该公钥调用服务。然而,这并不能完全识别客户。任何人都可以获得您的公钥。
- 相互证书或 2 路 SSL:这是当客户端拥有私钥并将公钥提供给服务时。反之亦然,即服务将其公钥提供给客户端。
这取决于您需要什么级别的身份验证。对于极少数客户,用户名/密码就足够了。 (总是有丢失的风险)
对于主要客户,2 种方式的 SSL 非常安全,因为人们不会轻易丢失私钥。
根据您的选择,可以共享更多代码示例。
对于选项 #,请点击此链接。 (您可以在以下步骤中使用您的 SSL 证书)
关于c# - 启用 HTTPS、IIS 托管的 WCF 服务,如何保护它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22849998/