1
我想用用户名/密码设置wsHttpBinding
。 我遇到的问题是如何设置用户名/密码?用用户名密码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!";
}
}
}
我必须在代码之前添加一些东西吗?我在第一行收到空引用异常。 – FrenkyB
基本上问题是这是null:myServiceHost.Description.Behaviors.Find()。 –
FrenkyB
然后,只需将新的ServiceCredentials行为添加到myServiceHost.Description.Behaviors –