Web Services 我遇到此错误系统.服务模型.安全.消息安全异常

pgccezyw  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(147)

就我所阅读和理解的内容而言,当我不发送身份验证时会发生这种情况。
但我试着用两种方式发送它:

string userN = "username";
string _pasw = "password";
BasicHttpBinding binding = new BasicHttpBinding();
Endpoint wsdl = new Endpoint("MyEndpoint");

SoapClient client = new SoapClient(binding, wsdl);
client.ClientCredentials.UserName.UserName = userN;
client.ClientCredentials.UserName.Password = _pasw;
await client.OpenAsync();

还有:

HttpClient request = new HttpClient();
var svcCredentianls = Encoding.ASCII.GetBytes(userN + ":" + _pasw);
request.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(svcCredentianls));

但无论如何我都有这样的错误:
系统.服务模型.安全性.消息安全性异常:'HTTP要求未受权使用“Anonymous”客户端验证配置。从服务器收到的验证信头是“Basic realm=“SAP NetWeaver Application Server [PRD/400]""。'
我哪里错了还是我错过了什么?

6ljaweal

6ljaweal1#

我认为问题出在web.config中的绑定配置。请检查安全节点。

<security mode="Transport">
  <transport clientCredentialType="Basic" proxyCredentialType="Basic" realm="" />
</security>

下面你可以找到我的工作例子:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="Transport">
                    <transport clientCredentialType="Basic" proxyCredentialType="Basic" realm="" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="https://pc:8080/Service.svc/SslService" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference1.IService"
            name="BasicHttpBinding_IService" />
    </client>
</system.serviceModel>

如果您不使用web.config配置,则可以尝试使用以下代码调整BasicHttpBinding创建代码:

BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.Transport;

相关问题