当使用基本身份验证的WCF basicHttpBinding时,我注意到IIS重置后的第一个请求是在没有用户/传递数据的情况下发送的(没有授权:基本....头数据)
代码:
client.ClientCredentials.UserName.UserName = "myUserName";
client.ClientCredentials.UserName.Password = "myPassword";
string anything = client.getValue(@"anyParam..");
配置:
<basicHttpBinding>
<binding name="ServiceNameHereServiceBinding" >
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" proxyCredentialType="None"
realm="">
</transport>
</security>
</binding>
</basicHttpBinding>
在Fidler监控之后,我发现,第一个请求始终返回401(完全不使用Authentication头),然后另一个请求出来并返回505错误。 之后,该服务将适用于所有进一步的请求。
答案 0 :(得分:0)
我找到了解决方案,我想我可以与你分享,这可能有所帮助。
斯坦尼斯拉夫·德沃伊琴科的解决方案是http://plainoldstan.blogspot.ca/2008/07/avoid-http-401-roundtrip-with-adding.html
只需自己创建基本身份验证标头,而不是依赖客户端来执行此操作。因为开箱即用的客户端会考虑端点已经预先验证。
// Assign client.ClientCredentials.UserName.UserName and client.ClientCredentials.UserName.Password
SetupClientAuthentication();
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " +
Convert.ToBase64String(Encoding.ASCII.GetBytes(client.ClientCredentials.UserName.UserName + ":" +
client.ClientCredentials.UserName.Password));
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] =
httpRequestProperty;
// Invoke client
}