gpt4 book ai didi

c# - 从客户端消息检查器访问 ClientCredential 属性

转载 作者:行者123 更新时间:2023-12-02 05:08:31 24 4
gpt4 key购买 nike

我可以从客户端消息检查器中引用代理客户端实例吗?

原因,我想访问以下属性的值:

ClientCredentials.UserName.UserName  
ClientCredentials.UserName.Password

谢谢

最佳答案

我通过从我的自定义 EndpointBehavior 传递对“ClientCredentials”的引用,设法从检查器中检索凭据:

自定义行为:

public class CustomEndpointBehaviour:IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}

public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
ClientCredentials credentials = endpoint.Behaviors.Find<ClientCredentials>();
clientRuntime.MessageInspectors.Add(new CustomMessageInspector(credentials));
}

public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
}

public void Validate(ServiceEndpoint endpoint)
{
}
}

检查员:

public class CustomMessageInspector : IClientMessageInspector
{
ClientCredentials crendentials = null;

public CustomMessageInspector(ClientCredentials credentials)
{
this.crendentials = credentials;
}

public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
}

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
string userName = "";
string passWord = "";

if (!(crendentials == null))
{
userName = crendentials.UserName.UserName;
passWord = crendentials.UserName.Password;
}
return null;
}
}

关于c# - 从客户端消息检查器访问 ClientCredential 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15875119/

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