繁体   English   中英

C#:添加WCF身份验证,用户名+ SSL的方法?

[英]C#: method to add WCF authentication, username + SSL?

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

远程WCF服务器正在运行传统Windows Service包装程序(即非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";

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

如果我做对了,你需要玩弄服务行为。 我在3.5 sp1中做到了这一点,我认为它应该与4.0相同。

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

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

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

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

C:\\ WF_WCF_Samples \\ WCF \\扩展性\\安全\\ UserNamePasswordValidator \\ CS

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM