WCF TransportCredentialOnly不发送用户名和密码

Kla*_*Nji 9 wcf

我有以下WCF客户端配置:

<basicHttpBinding>
   <binding name="basicHttpOCCWS" closeTimeout="00:01:00" openTimeout="00:01:00"
                 receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                 bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                 maxBufferSize="100000000" maxBufferPoolSize="524288"
                 maxReceivedMessageSize="100000000" messageEncoding="Text"
                 textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="8192"
                    maxArrayLength="16384" maxBytesPerRead="4096" 
                    maxNameTableCharCount="16384" />
      <security mode="TransportCredentialOnly">
         <transport clientCredentialType="Basic" />            
      </security>
   </binding>
</basicHttpBinding>
Run Code Online (Sandbox Code Playgroud)

在代码中,我按如下方式设置用户名和密码:

client.ClientCredentials.UserName.UserName = _cacledUserId;
client.ClientCredentials.UserName.Password = _cachedPassword;
Run Code Online (Sandbox Code Playgroud)

但是,在Tomcat上运行的Web服务返回错误:

"在安全上下文中找不到身份验证对象."

当我查看HTTP标头时,它缺少凭据信息,如下所示:

POST /occ600webservice/services/OCC_WS HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8; action=""
Host: 192.54.173.130:8080
Content-Length: 2223
Expect: 100-continue 
Run Code Online (Sandbox Code Playgroud)

为什么我的凭据没有被发送?

TIA.

克劳斯

Kla*_*Nji 21

对于遇到相同问题的其他人,请将每个调用包装到受保护的Web服务资源,如下所示:

var client = new WCClient(); 

using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
   var httpRequestProperty = new HttpRequestMessageProperty();
   httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = 
     "Basic " + 
     Convert.ToBase64String(Encoding.ASCII.GetBytes(
            client.ClientCredentials.UserName.UserName + ":" +
            client.ClientCredentials.UserName.Password));

   OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] =
               httpRequestProperty;

   client.DoSomething();
}
Run Code Online (Sandbox Code Playgroud)

请参阅以下链接:

  • 我有同样的问题.这是帮助我的唯一解决方案.如果我可以投票100次,我会的. (6认同)