How do configure username/password authentication for WCF netTcpBinding?
Asked Answered
A

1

6

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.

Anisotropic answered 13/10, 2010 at 10:47 Comment(0)
A
17

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";
Anisotropic answered 13/10, 2010 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.