【问题标题】:C#: method to add WCF authentication, username + SSL?C#:添加WCF身份验证,用户名+ SSL的方法?
【发布时间】:2010-11-22 23:15:23
【问题描述】:

我已经编写了一个 WCF 客户端和一个远程 Internet WCF 服务器。

远程 WCF 服务器正在运行托管在传统 Windows 服务包装器(即非 IIS)中的 WPF。

目前,它与基本的 HTTP 绑定完美配合。我正在使用 Visual Studio 2010 + .NET 4.0 + C#。

谁能指出正确的步骤来更改我的代码,以便我可以添加用户名 + SSL 身份验证?

编辑:

在服务端,我重写了 UserNamePasswordValidator 如下:

public class CustomUserNameValidator : UserNamePasswordValidator
{
  public override void Validate(string userName, string password)
  {
    Console.WriteLine("Got to here");
  }
}

在服务端,我指定了用户名验证类的链接:

  ServiceHost serviceHost = new ServiceHost(typeof(PhiFeedServer.PhiFeed)); // ,baseAddress);

  const bool passswordAuthentication = true;
  if (passswordAuthentication)
  {
    // These two lines switch on username/password authentication (see custom class "CustomUserNameValidator" in common file PhiFeed.svc.cs)
    // See http://msdn.microsoft.com/en-us/library/aa354513.aspx
    serviceHost.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
    serviceHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new CustomUserNameValidator();
  }

  // Start the service
  serviceHost.Open();

在客户端:

  EndpointAddress endpointAddress = new EndpointAddress("http://localhost:20000/PhiFeed?wdsl"); 
  BasicHttpBinding serviceBinding = new BasicHttpBinding();
  serviceBinding.ReceiveTimeout = new TimeSpan(0, 0, 120);
  proxy = new PhiFeedClient(serviceBinding, endpointAddress);

  proxy.ClientCredentials.UserName.UserName = "myusername";
  proxy.ClientCredentials.UserName.Password = "mypassword";

但是,当我运行所有程序时,它甚至都不会调用用户名验证器——这是怎么回事?

【问题讨论】:

  • 它在 basichttp 上有效吗?我个人更喜欢 wsHttpBinding 或 net.tcp 绑定。因为您可以使用证书:)。那时它将被保护
  • 我当前的设置使用 wsHttpBinding,所以它可以使用证书。上面的代码不能正常工作。

标签: c# wcf wcf-security wcf-client


【解决方案1】:

如果我做对了,您将需要考虑服务行为。我在 3.5 sp1 中这样做了,我认为在 4.0 中应该是一样的。

阅读: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/7d589542-277a-404e-ab46-222794422233/

【讨论】:

  • 正确答案如下,但如果您的问题可能给了我线索,那么回答我自己的问题似乎有点不妥:)
【解决方案2】:

啊哈!找到了我的问题的解决方案。

Microsoft 提供了示例代码,演示了如何将用户名/密码 + SSL 身份验证添加到控制台应用程序。

搜索“适用于 .NET Framework 4 的 Windows Communication Foundation (WCF) 和 Windows Workflow Foundation (WF) 示例”,下载并解压缩到 C:,然后在此处运行示例:

C:\WF_WCF_Samples\WCF\Extensibility\Security\UserNamePasswordValidator\CS

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多