我有一个带Net.Tcp绑定的WCF服务,我的服务器配置是
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="Service.PlainUserNameValidator, Service" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Service.TestService">
<endpoint address="" binding="netTcpBinding" contract="Service.ITestService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8732/Service/TestService/" />
<add baseAddress="http://localhost:8001/service/TestServiceMex/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
在配置中,我打开了customUserNamePasswordValidatorType
。启动主机的代码是
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(TestService)))
{
host.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine(String.Format("Metadata is at {0}?WSDL", host.Description.Endpoints[0].Address));
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
}
}
}
我在配置文件中注册的自定义用户名验证器类是
namespace Service
{
using System;
using System.IdentityModel.Selectors;
public class PlainUserNameValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
Console.WriteLine("Requesting username {0} and password {1}.", userName, password);
}
}
}
然而,当客户端正在呼叫时,验证器似乎永远不会触发。
我需要注意哪些特殊技巧才能为customUserNamePasswordValidatorType
绑定启用Net.Tcp
?
答案 0 :(得分:1)
两点:
您没有包含证书,您应该这样做以确保客户端凭据的完整性,如果必须,请设置临时凭证并将其添加到您的服务凭证中。
<serviceCertificate
findValue="localhost"
x509FindType="FindBySubjectName"
storeLocation="CurrentUser"
storeName="My" />
您还没有指定任何安全性 - 为此,客户端和服务端点都需要在其绑定上启用用户名和密码身份验证模式
<netTcpBinding>
<binding name="tcpWithMessageSecurity">
<security mode="Message" >
<message clientCredentialType="UserName"/>
</security>
</binding>
</netTcpBinding>
然后
<endpoint address="" binding="tcpWithMessageSecurity" contract="Service.ITestService">
然后,为您的行为命名,并将其添加到上一行。