WCF 服务的 UsernameTokenHeader [英] UsernameTokenHeader for WCF service

查看:25
本文介绍了WCF 服务的 UsernameTokenHeader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的 WCF 服务.在这里,我试图在 wcf 请求消息中添加 wsse:Security UsernameToken Header,但我不知道如何做到这一点以及其他事情.如果有人能对此提供帮助,将不胜感激.提前致谢.

I am new WCF service. Here I am trying to add wsse:Security UsernameToken Header in wcf request message but I don't know how to do that and the rest of the things. It would be much appreciated if someone could help on this. Thanks in advance.

推荐答案

您可以通过实现 IDispatchMessageInspector 接口来添加自定义标头.IDispatchMessageInspector 是服务器实现的接口.客户端需要实现 IClientMessageInspector 接口.

You can add a custom header by implementing the IDispatchMessageInspector interface. IDispatchMessageInspector is the interface implemented by the server. The client needs to implement the IClientMessageInspector interface.

这是我的演示:

public class CustomMessageInspector : IDispatchMessageInspector
    {
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            return null;
        }

        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            MessageHeader header = MessageHeader.CreateHeader("Username", "http://Username", "Username");
            OperationContext.Current.OutgoingMessageHeaders.Add(header);
        }
    }

这是SOAP消息中添加的header.如果需要给http请求添加header,参考如下代码:

This is the header added in the SOAP message.If you need to add a header to the http request, refer to the following code:

public class CustomMessageInspector : IDispatchMessageInspector
        {
            public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
            {

                return null;
            }

            public void BeforeSendReply(ref Message reply, object correlationState)
            {
                WebOperationContext Context = WebOperationContext.Current;
                Context.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
            }
        }

CustomMessageInspector 实现了 IDispatchMessageInspector 接口,在获取到消息后给消息添加了一个自定义的 header,在发送消息之前也添加了一个自定义的 header.

CustomMessageInspector implements the IDispatchMessageInspector interface, and adds a custom header to the message after getting the message, and also adds a custom header before sending the message.

 [AttributeUsage(AttributeTargets.Interface)]
    public class CustomBehavior : Attribute, IContractBehavior
    {
        public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
            return;
        }

        public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            return;
        }
    public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    {
      dispatchRuntime.MessageInspectors.Add(new CustomMessageInspector());
    }

    public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    {
        return;
    }
}

我们将此拦截器添加到服务的行为中.

We add this interceptor to the behavior of the service.

最后,我们将 Custombehavior 应用到我们的服务中.

Finally we apply Custombehavior to our service.

这篇关于WCF 服务的 UsernameTokenHeader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆