I have a WCF service with Net.Tcp binding, my server configuration is
<?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>
In the configuration I turned on customUserNamePasswordValidatorType. The code to start the host is
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();
}
}
}
And my custom username validator class registered in config file is
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);
}
}
}
However the validator never seems to fire while the client is calling.
Any special trick I need to notice to enable customUserNamePasswordValidatorType for Net.Tcp binding?
Two points:
You havent included a certificate, you should do so to ensure the integrity of the client’s credentials, set up a temporary one if you have to and add this to your service credentials.
You also havent specified any security – for this to work, both client and service endpoints need to enable Username and Password authentication mode on their bindings
then
Then, name your behavior and add it in the above line.