【问题标题】:How do configure username/password authentication for WCF netTcpBinding?如何为 WCF netTcpBinding 配置用户名/密码身份验证?
【发布时间】:2011-04-24 18:47:51
【问题描述】:

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

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

【问题讨论】:

    标签: wcf authentication nettcpbinding


    【解决方案1】:

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

    服务主机:

            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";
    

    【讨论】:

      猜你喜欢
      • 2011-01-13
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 2011-10-23
      • 1970-01-01
      • 2023-03-31
      • 2018-12-02
      • 2018-07-13
      相关资源
      最近更新 更多