WCF安全的配置

1,添加 System.IdentityModel 引用dll

2,添加一个继承UserNamePasswordValidator的类,来验证用户名,密码,并且实现其抽象方法Validate,异常类型为FaultException

3,配置文件中的添加如下内容:
(1),

<behavior name="DomeBehavior">
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="WcfServer.UserValidator,WcfServer"/>
</serviceCredentials>
</behavior>

(2)

<binding name="TransportSecurityBinding">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName"/>
</security>
</binding>

(3)

<service name="WcfService.Dome" behaviorConfiguration="DomeBehavior">
<endpoint binding="basicHttpBinding" behaviorConfiguration="TransportSecurityBinding" contract="WcfService.IDome"></endpoint>
</service>

注意:name="WcfService.Dome"为 命名空间.类名称,这个命名规则不能变,要不然就会出现不能访问元数据的异常

4,访问协议必须基于https

5,必须添加证书,在客户端调用时,若不想验证证书可通过 在 ServicePointManager.ServerCertificateValidationCallback 事件中,返回true来实现

ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
{
return true;
};

还得配置

<binding name="BasicHttpBinding_IDome">
<security mode="TransportWithMessageCredential" />
</binding>

6,至此wcf服务,基于https协议的访问,才算是安全的
以上内容即为本人在学习中的日记,不做教学使用。

原文地址:https://www.cnblogs.com/wushaoliang/p/4284173.html