我正在搜索 wcf 服务中 nettcp 绑定的自定义用户名/密码身份验证示例,并将其托管在控制台应用程序上,但我找不到任何东西。所以我说要为自己写一个例子。
我得到了这个示例并将其更改为使用 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 服务并将其托管在控制台应用程序上?