代码之家  ›  专栏  ›  技术社区  ›  jgauffin

如何为wcf nettcpbinding配置用户名/密码验证?

  •  6
  • jgauffin  · 技术社区  · 14 年前

    我希望能够使用nettcpbinding的用户名/密码身份验证,这是可能的吗?(用户名密码验证程序或类似的东西),没有Windows身份验证。

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

    1 回复  |  直到 10 年前
        1
  •  16
  •   jgauffin    14 年前

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

    服务主机:

            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";
    
    推荐文章