从WebApi调用WCF服务会抛出MessageSecurityException

时间:2016-03-02 08:15:00

标签: asp.net-web-api2 wcf-security

我使用第三方客户端库,它使用WCF在内部与服务器(云中的某个地方)进行通信(我只能配置几个键/值设置,库在其代码中创建所有WCF客户端代理堆栈。)

如果我在WinForms或控制台应用程序中使用它,它运行良好,但是从WebApi中调用库API最终会出错:

An exception of type 'System.ServiceModel.Security.MessageSecurityException' occurred in mscorlib.dll but was not handled in user code

Additional information: The Identity check failed for the outgoing message. 
The remote endpoint did not provide a domain name system (DNS) claim and therefore did not satisfied DNS identity 'serverName'. 
This may be caused by lack of DNS or CN name in the remote endpoint X.509 certificate's distinguished name.

为什么会有区别?服务器端是相同的,这是否意味着在WinForms应用程序中运行时不执行身份检查?或者在WebApi中运行时,身份检查的执行方式有所不同?我该如何解决?

我在VS2015中运行代码,WebApi托管在IISExpress中。该库使用NetTcpBinding TransportWithMessageCredentialMessageCredentialType.UserName

1 个答案:

答案 0 :(得分:3)

尝试将此添加到您的app.config文件

<configuration>
    <runtime>
        <AppContextSwitchOverrides value="Switch.System.IdentityModel.DisableMultipleDNSEntriesInSANCertificate=true" />
    </runtime>
</configuration>

来自MSDN:

  

从面向.NET Framework 4.6.1的应用程序开始,X509CertificateClaimSet.FindClaims方法将尝试将claimType参数与其SAN字段中的所有DNS entires进行匹配。   冲击

     

此更改仅影响面向.NET Framework 4.6.1的应用程序。

     

对于以.NET Framework早期版本为目标的应用程序,X509CertificateClaimSet.FindClaims方法尝试仅将claimType参数与最后一个DNS条目进行匹配。   减轻

     

如果不希望这种更改,那么以.NET Framework 4.6.1为目标的应用可以通过将以下配置设置添加到应用配置文件的部分来选择退出:

https://msdn.microsoft.com/en-us/library/mt620030%28v=vs.110%29.aspx

相关问题