<i id='Ek8sT'><tr id='Ek8sT'><dt id='Ek8sT'><q id='Ek8sT'><span id='Ek8sT'><b id='Ek8sT'><form id='Ek8sT'><ins id='Ek8sT'></ins><ul id='Ek8sT'></ul><sub id='Ek8sT'></sub></form><legend id='Ek8sT'></legend><bdo id='Ek8sT'><pre id='Ek8sT'><center id='Ek8sT'></center></pre></bdo></b><th id='Ek8sT'></th></span></q></dt></tr></i><div id='Ek8sT'><tfoot id='Ek8sT'></tfoot><dl id='Ek8sT'><fieldset id='Ek8sT'></fieldset></dl></div>

    1. <small id='Ek8sT'></small><noframes id='Ek8sT'>

        <bdo id='Ek8sT'></bdo><ul id='Ek8sT'></ul>
    2. <tfoot id='Ek8sT'></tfoot>

    3. <legend id='Ek8sT'><style id='Ek8sT'><dir id='Ek8sT'><q id='Ek8sT'></q></dir></style></legend>
    4. 如何向 WCF 中的请求添加授权标头?

      时间:2023-08-26
        • <bdo id='r920S'></bdo><ul id='r920S'></ul>

              <tbody id='r920S'></tbody>
            <tfoot id='r920S'></tfoot><legend id='r920S'><style id='r920S'><dir id='r920S'><q id='r920S'></q></dir></style></legend>

          1. <small id='r920S'></small><noframes id='r920S'>

            <i id='r920S'><tr id='r920S'><dt id='r920S'><q id='r920S'><span id='r920S'><b id='r920S'><form id='r920S'><ins id='r920S'></ins><ul id='r920S'></ul><sub id='r920S'></sub></form><legend id='r920S'></legend><bdo id='r920S'><pre id='r920S'><center id='r920S'></center></pre></bdo></b><th id='r920S'></th></span></q></dt></tr></i><div id='r920S'><tfoot id='r920S'></tfoot><dl id='r920S'><fieldset id='r920S'></fieldset></dl></div>
              • 本文介绍了如何向 WCF 中的请求添加授权标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在开发一个 Windows 窗体应用程序,并且需要调用一个 WCF 服务.在将请求发送到服务之前,我需要向请求添加标头(授权 - 自定义).我也有一个自定义检查器类.我尝试了以下方法,但不知何故未调用该服务,它返回异常.

                I'm working on a Windows Form application and there's a WCF service that needs to be called. I need to add a header (authorization - custom) to the request before it's sent to the service. I have a custom inspector class as well. I tried the following but the service is not called, somehow, and it returns an exception.

                public object BeforeSendRequest(ref Message request, IClientChannel channel)
                {
                    MessageHeader header = MessageHeader.CreateHeader("Authorization", "", "Basic Y19udGk6Q29udGlfQjNTVA==");
                    OperationContext.Current.OutgoingMessageHeaders.Add(header);
                    HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
                    httpRequestProperty.Headers.Add("Authorization", "Basic Y19udGk6Q29udGlfQjNTVA==");
                    httpRequestProperty.Headers.Add(HttpRequestHeader.UserAgent, "Continental");
                            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
                    sentMessages.Add(request.ToString());
                    return null;
                }
                

                我也试过这样最简单的方法:

                I also tried simplest way like this one:

                MessageHeader header = MessageHeader.CreateHeader("Authorization", "", "Basic Y19udGk6Q29udGlfQjNTVA==");
                request.Headers.Add(header);
                

                但是一样,添加了授权头但没有到达服务,我怎么知道服务收到了什么头?当我在请求中手动添加这样的标头时(在运行之前),我使用了 SOAP UI 并且服务响应良好.

                but it's the same, authorization header is added but it does not reach the service, how can I know what header is received by the service? I used SOAP UI and service responds well when I add such a header manually in the request (before running).

                推荐答案

                最简单的方法是在客户端添加:

                using (MyServ.ServiceClient proxy = new MyServ.ServiceClient())
                {
                     using (new System.ServiceModel.OperationContextScope(proxy.InnerChannel))
                     {
                         MessageHeader head = MessageHeader.CreateHeader("Authorization", "http://yournamespace.com/v1", data);
                         OperationContext.Current.OutgoingMessageHeaders.Add(head);
                     }
                }
                

                在服务器端检索它:

                string  auth = OperationContext.Current.IncomingMessageHeaders.
                GetHeader<string>("Authorization", "http://mynamespace.com/v1");
                

                我还建议您查看这些文章:

                I also suggest that you check these articles:

                使用 WCF 的 Http 请求中缺少授权标头

                使用 wsHttpBinding 的 WCF 服务 - 操作 HTTP 请求标头

                这篇关于如何向 WCF 中的请求添加授权标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如果我在 C# 中发送 0 有效载荷数据,udp 数据包的 下一篇:WPF:如何冻结 ListView 标题行,使其不会滚动到屏幕

                相关文章

                最新文章

                1. <legend id='t2aR2'><style id='t2aR2'><dir id='t2aR2'><q id='t2aR2'></q></dir></style></legend>

                    <tfoot id='t2aR2'></tfoot>

                    <small id='t2aR2'></small><noframes id='t2aR2'>

                    <i id='t2aR2'><tr id='t2aR2'><dt id='t2aR2'><q id='t2aR2'><span id='t2aR2'><b id='t2aR2'><form id='t2aR2'><ins id='t2aR2'></ins><ul id='t2aR2'></ul><sub id='t2aR2'></sub></form><legend id='t2aR2'></legend><bdo id='t2aR2'><pre id='t2aR2'><center id='t2aR2'></center></pre></bdo></b><th id='t2aR2'></th></span></q></dt></tr></i><div id='t2aR2'><tfoot id='t2aR2'></tfoot><dl id='t2aR2'><fieldset id='t2aR2'></fieldset></dl></div>
                      <bdo id='t2aR2'></bdo><ul id='t2aR2'></ul>