HTTP 请求未经客户端身份验证方案“匿名”授权。从服务器收到的身份验证标头是“基本领域”

Posted

技术标签:

【中文标题】HTTP 请求未经客户端身份验证方案“匿名”授权。从服务器收到的身份验证标头是“基本领域”【英文标题】:The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic Realm’ 【发布时间】:2016-07-15 23:46:08 【问题描述】:

我正在尝试使用 SAP WCF 网络服务,

我有这个代码:

var remoteAddress = new System.ServiceModel.EndpointAddress("https://appweb.xx.com/sap/bc/srt/wsdl/bndg_E5BBCE4ACBB46CF1A88900155D0A8410/wsdl11/allinone/ws_policy/document?sap-client=300");

                using (var cliente = new ServiceReference1.ZHRWS_GET_CERTIFICADOClient(new System.ServiceModel.BasicHttpsBinding(), remoteAddress))
                
                     //set timeout
                    cliente.Endpoint.Binding.SendTimeout = new TimeSpan(0, 0, 0, 6000);
                    cliente.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
                    cliente.ClientCredentials.Windows.ClientCredential.UserName = "xx";
                    cliente.ClientCredentials.Windows.ClientCredential.Password = "xx";
                    ServiceReference1.ZhrGetCertificado cert = new ServiceReference1.ZhrGetCertificado();
                    cert.ICtflab = "1";
                    cert.IIcnum = "8026347";
                    //call web service method
                    var  response = cliente.ZhrGetCertificado(cert);
                

在我的 app.config 中有这个:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <system.serviceModel>
        <bindings>
             <basicHttpBinding>
                <security mode="TransportCredentialOnly">
                      <transport clientCredentialType="Basic" proxyCredentialType="None" realm="" />
                      <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>     
           </basicHttpBinding>
            <customBinding>
                <binding name="ZWSLINK">
                    <!--    WsdlImporter encountered unrecognized policy assertions in ServiceDescription 'urn:sap-com:document:sap:soap:functions:mc-style':    -->
                    <!--    <wsdl:binding name='ZWSLINK'>    -->
                    <!--        <wsaw:UsingAddressing xmlns:wsaw="http://schemas.xmlsoap.org/ws/2004/08/addressing">..</wsaw:UsingAddressing>    -->
                    <!--        <saptrnbnd:OptimizedXMLTransfer xmlns:saptrnbnd="http://www.sap.com/webas/710/soap/features/transportbinding/">..</saptrnbnd:OptimizedXMLTransfer>    -->
                    <!--        <sapattahnd:Enabled xmlns:sapattahnd="http://www.sap.com/710/features/attachment/">..</sapattahnd:Enabled>    -->
                    <textMessageEncoding messageVersion="Soap11" />
                    <httpsTransport authenticationScheme="Basic" />
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint address="https://appweb.xx.com/sap/bc/srt/wsdl/bndg_E5BBCE4ACBB46CF1A88900155D0A8410/wsdl11/allinone/ws_policy/document?sap-client=300"
                binding="customBinding" 
                bindingConfiguration="ZWSLINK" 
                contract="ServiceReference1.ZHRWS_GET_CERTIFICADO"
                name="ZWSLINK" />
        </client>
    </system.serviceModel>
</configuration>

但是我仍然收到此错误:

HTTP 请求未经客户端身份验证方案“匿名”授权。从服务器收到的身份验证标头是“Basic Realm”

【问题讨论】:

【参考方案1】:

Basic authentication ,我假设尝试使用基本身份验证发送用户名和密码,而不是在 http 中。请试试这个

<binding name="...">
    <security mode="TransportWithMessageCredential" >
       <message clientCredentialType="UserName" />
    </security>
</binding>

【讨论】:

以上是关于HTTP 请求未经客户端身份验证方案“匿名”授权。从服务器收到的身份验证标头是“基本领域”的主要内容,如果未能解决你的问题,请参考以下文章

客户端身份验证方案“匿名”的 HTTP 请求未经授权?

HTTP 请求未经客户端身份验证方案“匿名”授权。从服务器收到的身份验证标头是“默认”。

HTTP 请求未经客户端身份验证方案“匿名”授权。从服务器收到的身份验证标头是“基本领域”

WCF-TransportWithMessageCredential HTTP 请求未经授权,客户端身份验证方案“匿名”

未经授权的客户端身份验证方案“匿名”和 URI 方案“https”无效;预期的“http”

HTTP 请求未经客户端身份验证方案“Ntlm”授权