WCF TransportCredentialOnly不发送用户名和密码

时间:2009-10-09 16:31:45

标签: 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>

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

client.ClientCredentials.UserName.UserName = _cacledUserId;
client.ClientCredentials.UserName.Password = _cachedPassword;

但是,在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 

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

TIA。

克劳斯

2 个答案:

答案 0 :(得分: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();
}

请参阅以下链接:

答案 1 :(得分:2)

尝试更改security元素以包含指定clientCredentialType的消息元素为UserName,否则传输不知道您要使用您填写的ClientCredentials.UserName对象。

Book.first.unset(:name)
 => #<Book _id: 555231746c617a1c99030000, name: nil, author: "a1">