问题描述:

I am passing the TokenId as Soap Header for all the requests.

<soapenv:Header> <tem:TokenIdentity>12345</tem:TokenIdentity> </soapenv:Header>

for example I have 5 webmethods.

I would like that ValidateTokenId() method which shoule be called automatically before accessing any webmethods.

Anybody done this before?

网友答案:

I got the solution to validate the token

WCF Service implemented(IDispatchMessageInspector) the following two methods to take care of Soap header validation and Logging the SOAP Requests and SOAP Responses.

AfterReceiveRequest So all the incoming SOAP requests are automatically called for ValidateToken() method and will be logged too.

BeforeSendReply All the response SOAP messages are logged here.

 #region IDispatchMessageInspector Members
    public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        int headerIndex1 = OperationContext.Current.IncomingMessageHeaders.FindHeader("TokenIdentity", "");
        XmlReader r = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(0).ReadSubtree();
        XElement data = XElement.Load(r);
        var tokenValue = (string)data;

        ValidateToken(tokenValue);

        //Log the Request with Log4Net or something
        //Console.WriteLine("IDispatchMessageInspector.AfterReceiveRequest called.");
        return null;
    }

    public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    {
 //Log the Response with Log4Net or something
        //Console.WriteLine("IDispatchMessageInspector.BeforeSendReply called.");
    }
 #endregion
相关阅读:
Top