如何为WCF netTcpBinding配置用户名/密码身份验证?

如何为WCF netTcpBinding配置用户名/密码身份验证?,wcf,authentication,nettcpbinding,Wcf,Authentication,Nettcpbinding,我希望能够通过nettcpbinding使用用户名/密码身份验证,这可能吗?(UserNamePasswordValidator或类似的东西),没有windows身份验证 我正在使用代码配置所有内容,因此请在任何示例中仅使用代码,而不要使用app.config。这是我想到的,我不知道是否不需要某些代码: 服务主机: ServiceHost host = new ServiceHost(concreteType); var binding = new NetTcpB

我希望能够通过nettcpbinding使用用户名/密码身份验证,这可能吗?(UserNamePasswordValidator或类似的东西),没有windows身份验证


我正在使用代码配置所有内容,因此请在任何示例中仅使用代码,而不要使用app.config。

这是我想到的,我不知道是否不需要某些代码:

服务主机:

        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;
和客户端:

        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";
var binding=new NetTcpBinding(SecurityMode.TransportWithMessageCredential,true);
binding.Security.Message.ClientCredentialType=MessageCredentialType.UserName;
var工厂=新渠道工厂(绑定,
新端点地址(
新Uri(“net。tcp://someaddress:9000/switch")));
factory.Credentials.UserName.UserName=“myUserName”;
factory.Credentials.UserName.Password=“myPassword”;