wcf - 使用 WCF 的 Http 请求中缺少授权 header

标签 wcf http-headers

我正在使用 WCF 访问 Web 服务。使用 WSHttpBinding,安全模式设置为传输 (https),​​客户端凭据类型为基本。当我尝试使用代理访问服务时,收到 401 未经授权的异常。

这里是绑定(bind)

var binding = new WSHttpBinding()
        {
            UseDefaultWebProxy = true,
            Security =
            {
                Mode = SecurityMode.Transport,
                Transport =
                {
                    ClientCredentialType = HttpClientCredentialType.Basic,
                },
            }
        };

这是服务电话
var client = new InternetClient(binding, new EndpointAddress("httpsurl"));

        client.ClientCredentials.UserName.UserName = "username";
        client.ClientCredentials.UserName.Password = "password";
        client.ProcessMessage("somevalue");

使用 Http Analyzer 查看 Http header 时
连接头

(请求行):CONNECT somehost.com:443 HTTP/1.1
主持人:somehost.com
代理连接:保持事件

帖子标题

(请求行):POST/Company/1.0 HTTP/1.1
内容类型:应用程序/soap+xml;字符集=utf-8
VsDebuggerCausalityData:uIDPo+voStemjalOv5LtRotFQ7UAAAAAUKLJpa755k6oRwto14BnuE2PDtYKxr9LhfqXFSOo8pEACQAA
主持人:somehost.com
内容长度:898
期望:100-继续
连接:保活

如果您看到 header 授权 header 丢失

现在我的问题是为什么 WCF 调用缺少授权 header ?我错过了什么吗? .请询问您是否需要更多信息

最佳答案

我有完全相同的问题。我能够使用以下代码手动注入(inject)授权 header :

var callcontext = new CAdxCallContext();
callcontext.codeLang = "ENG";
callcontext.poolAlias = "BGRTEST";

var proxy = new CAdxWebServiceXmlCCClient();
proxy.Endpoint.EndpointBehaviors.Add(new CustomEndpoint()); 
proxy.ClientCredentials.UserName.UserName = "USERNAME"; // Might not benecessary
proxy.ClientCredentials.UserName.Password = "PASSWORD"; // Might not benecessary

string inputXml = "<PARAM>" +
       "<GRP ID= \"GRP1\">" +
       "<FLD NAME = \"ITMREF\">" + "100001" + "</FLD>" +
       "</GRP>" +
       "</PARAM>";

CAdxResultXml response;
try
{
    response = proxy.run(callcontext, "BGR_SIEPRO", inputXml);
}
catch (TimeoutException timeout)
{
    Console.WriteLine(timeout.Message);
    // handle the timeout exception.
    proxy.Abort();
}
catch (CommunicationException commexception)
{
    Console.WriteLine(commexception.Message);
    // handle the communication exception.
    proxy.Abort();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
finally
{
     proxy.Close();
}
}

    public class ClientMessageInspector : IClientMessageInspector
    {
        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            // Nothing Here
            Console.Write(reply.ToString());
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
            httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " +
                Convert.ToBase64String(Encoding.ASCII.GetBytes("USERNAME" + ":" +
                    "PASSWORD"));
            request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestProperty);
            return null;
        }
    }

    public class CustomEndpoint : IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
            // Nothing here
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.ClientMessageInspectors.Add(new ClientMessageInspector());
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            // Nothing here
        }

        public void Validate(ServiceEndpoint endpoint)
        {
            // Nothing here
        }
    }

关于wcf - 使用 WCF 的 Http 请求中缺少授权 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4080986/

相关文章:

wcf - 我应该在每次服务调用后打开和关闭我的 WCF 客户端吗?

c# - WCF over TCP 值得吗?

WCF 未绑定(bind)到正确的 IP 地址

c# - WCF 全双工处理客户端断开连接

c# - WCF 中的证书验证

c# - 如何防止 HttpClient 发送连接 header

http-headers - X-P2P-PeerDist header : where does it come from?

php - 401 - 尝试使用 PHP 将图片上传到 TwitPic 时出现 "Could not authenticate you (header rejected by twitter)."

java - Play Framework 停留在同一页面然后更改语言

Java Socket 编程 - HTTP 1.1 的 301 错误