gpt4 book ai didi

c# - wsHttpBinding 与用户名密码

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:15 27 4
gpt4 key购买 nike

我正在尝试使用用户名/密码设置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!";
}
}
}

最佳答案

默认情况下,WCF 使用 Windows 用户进行身份验证。但您可以插入您自己的自定义验证器。

您需要创建继承自 UserNamePasswordValidator 的类,然后告诉 WCF 使用它:

myServiceHost.Description.Behaviors.Find<ServiceCredentials>().UserNameAuthentication
.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
myServiceHost.Description.Behaviors.Find<ServiceCredentials>().UserNameAuthentication
.CustomUserNamePasswordValidator = new MyCustomValidator();

另见 this exampleofficial MSDN documentation .

关于c# - wsHttpBinding 与用户名密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44280295/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com