【问题标题】:Accessing a WCF MessageHeader when OperationContext.Current is null当 OperationContext.Current 为 null 时访问 WCF MessageHeader
【发布时间】:2010-08-23 18:49:44
【问题描述】:

我有一个使用自定义 UserNamePasswordValidator 保护的 WCF。我需要访问通常可用的内容:

OperationContext.Current.RequestContext.RequestMessage.Headers.To

所以我可以解析 URL。 但是,OperationContext.Current 为空。有没有办法在没有 OperationContext 的情况下获取消息头?

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    是的,可以通过消息检查器实现。

    OperationContextUserNamePasswordValidator.Validate 方法期间不可用,因为它将在管道中稍后在调用已分派到适当的服务方法时创建。

    通常,您会在 WCF 管道的早期使用 Message Inspectors 拦截传入和传出消息。但是,这不适用于您的情况,因为Message Inspectors are invoked only after the request has successfully been authenticated

    如果您需要在身份验证之前检查传入的 HTTP 请求,您唯一的选择是在以 ASP.NET 兼容模式运行的 IIS 中托管您的 WCF 服务。 这样您就可以通过 HttpContext 类访问请求的 URL:

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

    相关资源:

    【讨论】:

    • 做了一个快速测试,并在我的本地 IIS 上以 ASP.NET 兼容模式托管了一项服务,而 HttpContext.Current 仍然为空。看起来我必须在每个服务调用中进行身份验证:(
    • 看起来我无法在每个服务调用中进行身份验证...没有本地支持在 UserNamePasswordValidator.Validate 方法之外获取用户密码。
    • 将服务从 .NET 3.5 升级到 4.0 并且您的解决方案有效。谢谢!
    猜你喜欢
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    相关资源
    最近更新 更多