相关疑难解决方法(0)

带有 wsHttpBinding 的 WCF 服务 - 操作 HTTP 请求标头

我一直在关注教程,以便在我的 WCF 服务中使用传输安全进行用户名身份验证。然而,本教程提到使用basicHttpBinding哪个是不可接受的 - 我需要wsHttpBinding.

这个想法是BasicAuthenticationModule对 WCF 服务进行自定义,该服务将从 HTTP 请求中读取“授权”标头并根据“授权”标头内容执行身份验证过程。问题是缺少“授权”标题!

我已IClientMessageInspector通过自定义行为实现,以便操作传出消息并添加自定义 SOAP 标头。我在BeforeSendRequest函数中添加了以下代码:

    HttpRequestMessageProperty httpRequest = request.Properties.Where(x => x.Key == "httpRequest").Single().Value;
    httpRequest.Headers.Add("CustomHeader", "CustomValue");
Run Code Online (Sandbox Code Playgroud)

这应该有效,并且根据许多网络资源,它适用于basicHttpBinding但不适用于wsHttpBinding. 当我说“有效”时,我的意思是 WCF 服务成功接收到标头。

这是在 WCF 服务端检查接收到的 HTTP 消息的简化函数:

    public void OnAuthenticateRequest(object source, EventArgs eventArgs)
    {
        HttpApplication app = (HttpApplication)source;

        //the Authorization header is checked if present
        string authHeader = app.Request.Headers["Authorization"];
        if (string.IsNullOrEmpty(authHeader))
        {
            app.Response.StatusCode = 401;
            app.Response.End();
        }
    }
Run Code Online (Sandbox Code Playgroud)

2011 …

c# wcf basic-authentication wshttpbinding http-headers

5
推荐指数
1
解决办法
2万
查看次数