我将TokenId作为所有请求的Soap Header传递。
<soapenv:Header> <tem:TokenIdentity>12345</tem:TokenIdentity> </soapenv:Header>
例如,我有5个webmethods。
我希望在访问任何webmethods之前自动调用ValidateTokenId()方法。
之前有人这样做过吗?
答案 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