Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do configure username/password authentication for WCF netTcpBinding?

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

I'm configuring everything with code, so please only use code and not app.config in any examples.

like image 832
jgauffin Avatar asked Oct 13 '10 10:10

jgauffin


People also ask

How to configure a WCF service to authenticate using Windows domain username and password?

To configure a WCF service to authenticate using Windows domain username and password 1 Create an instance of the WSHttpBinding, set the security mode of the binding to WSHttpSecurity.Message, set the... 2 Specify the server certificate used to encrypt the username and password information sent over the wire. This code... More ...

What is a WCF user name and password validator?

Thank you. By default, when a user name and password is used for authentication, Windows Communication Foundation (WCF) uses Windows to validate the user name and password. However, WCF allows for custom user name and password authentication schemes, also known as validators.

How do I configure a wshttpbinding service to authenticate its clients?

If you would like to see an example of configuring a similar service using a configuration file, see Message Security User Name. To configure a service to authenticate its clients using Windows Domain username and passwords use the WSHttpBinding and set its Security.Mode property to Message.

What happens if usernamepasswordvalidationmode is not set in WCF?

If the userNamePasswordValidationMode value is not set, WCF uses Windows authentication instead of the custom user name and password validator. Set the customUserNamePasswordValidatorType to the type that represents your custom user name and password validator.


1 Answers

This is what I came up with, I have no idea if some of the code is not 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 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";
like image 186
jgauffin Avatar answered Nov 15 '22 11:11

jgauffin