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

发布于 2024-10-03 17:02:57 字数 1706 浏览 3 评论 0原文

我编写了 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";

但是,当我运行所有内容时,它甚至从未调用用户名验证器 - 发生了什么事?

I've written both a WCF client and a remote internet WCF server.

The remote WCF server is running WPF hosted in a traditional Windows Service wrapper (i.e. not IIS).

Currently, its working perfectly with basic HTTP binding. I'm using Visual Studio 2010 + .NET 4.0 + C#.

Can anyone point me in the direction of the right steps to alter my code so that I can add username + SSL authentication?

EDIT:

At the service end, I've overridden UserNamePasswordValidator as follows:

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

At the service end, I've specified a link to the username validation class:

  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();

At the client end:

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

However, when I run everything, it never even calls the username validator - whats going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末が日狂欢 2024-10-10 17:02:57

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

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

If i am getting this right, you will need to play around with service behaviour. I did that in 3.5 sp1 it should be the same in 4.0 i think.

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

花落人断肠 2024-10-10 17:02:57

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

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

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

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

Aha! Found the solution to my problem.

Microsoft provides example code which demonstrates how to add username/password + SSL authentication to a console app.

Search for "Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4", download, unzip into C:, then run the sample here:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文