如何在调用任何Web方法之前验证令牌ID?

时间:2013-05-21 14:33:02

标签: wcf wcf-security

我将TokenId作为所有请求的Soap Header传递。

<soapenv:Header> <tem:TokenIdentity>12345</tem:TokenIdentity>   </soapenv:Header>
例如,我有5个webmethods。 我希望在访问任何webmethods之前自动调用ValidateTokenId()方法。

之前有人这样做过吗?

1 个答案:

答案 0 :(得分:1)

我得到了验证令牌

的解决方案

WCF服务实现( IDispatchMessageInspector )以下两种方法来处理Soap头验证和 记录SOAP请求和SOAP响应。

<强> AfterReceiveRequest 因此,所有传入的SOAP请求都会自动调用ValidateToken()方法,并且也会被记录。

<强> BeforeSendReply 所有响应SOAP消息都记录在此处。

 #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