我正在尝试使用用户名/密码设置wsHttpBinding
。
我遇到的问题是如何设置用户名/密码?
我已经设定了这个:
binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
但用户名/密码的设置在哪里?
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
Type contractType = typeof(ITest);
Type serviceType = typeof(Test);
Uri httpUri = new Uri("http://localhost:8083/Test2");
ServiceHost myServiceHost = new ServiceHost(serviceType, httpUri);
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
myServiceHost.Description.Behaviors.Add(smb);
myServiceHost.Open();
代码:
namespace ConsoleApplication1
{
[ServiceContract]
interface ITest
{
[OperationContract]
string Ping();
}
}
namespace ConsoleApplication1
{
class Test : ITest
{
public string Ping()
{
return "Pong - it works!";
}
}
}
答案 0 :(得分:1)
默认情况下,WCF使用Windows用户进行身份验证。但您可以插入自己的自定义验证器。
您需要创建从UserNamePasswordValidator
继承的类,然后告诉WCF使用它:
myServiceHost.Description.Behaviors.Find<ServiceCredentials>().UserNameAuthentication
.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
myServiceHost.Description.Behaviors.Find<ServiceCredentials>().UserNameAuthentication
.CustomUserNamePasswordValidator = new MyCustomValidator();