我正在搜索wcf服务中nettcp绑定的自定义userName / Password身份验证的示例,并将其托管在控制台应用程序上,但我找不到任何内容。所以我说要为自己写一个例子。
我有this示例并将其更改为使用app.config。所以我创建了这个配置文件:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="GroceryListDuplexBehaviors">
<serviceMetadata httpGetEnabled="true" />
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="GroceryListDuplexServiceHost.MyValidator, GroceryListDuplexServiceHost"/>
<serviceCertificate storeLocation="LocalMachine" storeName="My"
x509FindType="FindBySubjectName" findValue="localhost"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="GroceryListDuplexBinding">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None"/>
<message clientCredentialType="UserName"/>
</security>
</binding>
</netTcpBinding>
</bindings>
<services>
<service behaviorConfiguration="GroceryListDuplexBehaviors"
name="ExampleDuplexServiceLibrary.GroceryListDuplexService">
<endpoint address="net.tcp://localhost:9011/WCFServices/" binding="netTcpBinding"
bindingConfiguration="GroceryListDuplexBinding" name="GroceryListDuplexService"
contract="ExampleDuplexServiceInterface.IGroceryListDuplexService" />
<endpoint binding="mexHttpBinding" bindingConfiguration="" name="GroceryListDuplexServiceMex"
kind="mexEndpoint" address="http://localhost:9001/WCFServices/mex" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:9001/WCFServices/" />
</baseAddresses>
</host>
</service>
</services>
所以我在本地系统上运行该服务并运行成功。然后我创建一个测试项目并使用这些配置文件添加该服务:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="certificateEndpointBehavior">
<clientCredentials>
<serviceCertificate>
<authentication certificateValidationMode="None" revocationMode="NoCheck"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="GroceryListDuplexService">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:9011/WCFServices/" binding="netTcpBinding" behaviorConfiguration="certificateEndpointBehavior"
bindingConfiguration="GroceryListDuplexService" contract="ServiceReference1.IGroceryListDuplexService"
name="GroceryListDuplexService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
客户端在本地计算机上运行成功,但是当我在远程计算机上运行客户端并想要连接到服务时,它会收到错误。
错误为An unsecured or incorrectly secured fault was received from the other party. See
the inner FaultException for the fault code and detail.
内部异常是An error occurred when verifying security for the message.
我的问题是如何使用nettcp绑定和自定义用户名/密码身份验证创建双工WCF服务并将其托管在控制台应用程序上?