首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从客户端消息检查器访问ClientCredential属性

从客户端消息检查器访问ClientCredential属性
EN

Stack Overflow用户
提问于 2013-04-08 17:00:03
回答 1查看 1.5K关注 0票数 1

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

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

代码语言:javascript
复制
ClientCredentials.UserName.UserName  
ClientCredentials.UserName.Password 

谢谢

EN

回答 1

Stack Overflow用户

发布于 2013-04-08 17:47:57

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

CustomBehaviour:

代码语言:javascript
复制
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)
        {

        }
    }

和检查员:

代码语言:javascript
复制
 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;
        }
    }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15875119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档