简体   繁体   English

当OperationContext.Current为null时访问WCF MessageHeader

[英]Accessing a WCF MessageHeader when OperationContext.Current is null

I have a WCF that is being secured using a custom UserNamePasswordValidator. 我有一个使用自定义UserNamePasswordValidator保护的WCF。 I need to access what normally would be available in: 我需要访问通常可用的内容:

OperationContext.Current.RequestContext.RequestMessage.Headers.To

so I can parse the URL. 所以我可以解析URL。 However, OperationContext.Current is null. 但是,OperationContext.Current为null。 Is there a way to get at the message header without OperationContext? 有没有办法在没有OperationContext的情况下获取邮件头?

Yes, it is possible through Message Inspectors. 是的,可以通过Message Inspectors。

The OperationContext is not available during the UserNamePasswordValidator.Validate method because it will be created later in the pipeline, when the call has been dispatched to the appropriate service method. 的OperationContext的过程中无法使用UserNamePasswordValidator.Validate方法,因为它会在管道中,当呼叫已被分派到相应的服务方法后创建的。

Normally you would intercept incoming and outgoing messages early in the WCF pipeline by using Message Inspectors . 通常,您可以使用Message Inspectors在WCF管道中尽早拦截传入和传出消息。 However this won't work in your case , since Message Inspectors are invoked only after the request has successfully been authenticated . 但是,这在您的情况下不起作用 ,因为仅在成功验证请求后才调用Message Inspectors

If you need to inspect the incoming HTTP request before authentication , your only option is to host your WCF service in IIS running in ASP.NET Compatibility Mode . 如果需要在身份验证之前检查传入的HTTP请求 ,则唯一的选择是在ASP.NET兼容模式下运行的IIS中托管WCF服务 This way you'll be able to access the request's URL through the HttpContext class: 这样您就可以通过HttpContext类访问请求的URL:

public override void Validate(string userName, string password)
{
    string url = HttpContext.Current.Request.Url.AbsoluteUri;
}

Related resources: 相关资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM