【发布时间】: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