How to configure username and password authentication for WCF netTcpBinding?

I would like to be able to use username and password authentication with nettcpbinding, is this possible? (UserNamePasswordValidator or something like that), there is no Windows authentication.

I configure everything with code, so please use only code, not app.config in any examples.

+6
authentication username wcf nettcpbinding
source share
1 answer

Here is what I came up with, I have no idea if any code is required:

Service Host:

ServiceHost host = new ServiceHost(concreteType); var binding = new NetTcpBinding(SecurityMode.TransportWithMessageCredential, true); binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; host.AddServiceEndpoint(serviceType, binding, "net.tcp://someaddress:9000/" + name); host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new CustomUserNameValidator(); host.Credentials.ServiceCertificate.Certificate = new X509Certificate2("mycertificate.p12", "password"); host.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom; 

And the client side:

  var binding = new NetTcpBinding(SecurityMode.TransportWithMessageCredential, true); binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; var factory = new ChannelFactory<ISwitchService>(binding, new EndpointAddress( new Uri("net.tcp://someaddress:9000/switch"))); factory.Credentials.UserName.UserName = "myUserName"; factory.Credentials.UserName.Password = "myPassword"; 
+14
source share

All Articles