简体   繁体   English

具有基本身份验证的basicHttpBinding发送没有用户/传递数据的第一个请求

[英]basicHttpBinding with basic authentication send first request with no user/pass data

When using WCF basicHttpBinding with basic authentication, I notice that the first request after IIS reset is sent without user/pass data (without Authorization: Basic .... Header data) 当使用带有基本身份验证的WCF basicHttpBinding时,我注意到IIS重置后的第一个请求是在没有用户/传递数据的情况下发送的(没有授权:Basic ....标头数据)

Code: 码:

client.ClientCredentials.UserName.UserName = "myUserName";
client.ClientCredentials.UserName.Password = "myPassword";
string anything = client.getValue(@"anyParam..");  

Config: 配置:

<basicHttpBinding>
    <binding name="ServiceNameHereServiceBinding" >
        <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Basic" proxyCredentialType="None"  
                    realm="">
            </transport>
        </security>
    </binding>
</basicHttpBinding>

After monitor by Fidler, I found that, the first request Always return 401 (go without Authentication header at all), then another request comes out and return 505 error. 在Fidler监视之后,我发现,第一个请求始终返回401(根本没有身份验证标头),然后另一个请求出现,并返回505错误。 After that the service will work good for all further requests. 之后,该服务将对所有进一步的请求都起作用。

I found the solution and I thought I may share it with you, it may help. 我找到了解决方案,我想可以与您分享,可能会有所帮助。

The solution is here http://plainoldstan.blogspot.ca/2008/07/avoid-http-401-roundtrip-with-adding.html by Stanislav Dvoychenko 解决方案在此处http://plainoldstan.blogspot.ca/2008/07/avoid-http-401-roundtrip-with-adding.html作者:Stanislav Dvoychenko

It's by simply create the basic authentication header yourself, instead of depend on the Client to do so. 只需自己创建基本身份验证标头,而不是依靠客户端来创建。 because out of the box client will consider end point is Pre-Authenticated already. 因为开箱即用的客户端将认为端点已经是预先认证的。

// 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 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM