Home > Software design >  I have this error System.ServiceModel.Security.MessageSecurityException
I have this error System.ServiceModel.Security.MessageSecurityException

Time:05-27

For what I read and understood, this happens when I'm not sending the authentication.

But I tried to send it in two ways:

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();

And:

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

But in one way or another I'm having this error:

System.ServiceModel.Security.MessageSecurityException: 'The HTTP request is not authorized with the "Anonymous" client authentication scheme. The authentication header received from the server was "Basic realm="SAP NetWeaver Application Server [PRD/400]"".'

Where I'm wrong or what I'm missing?

CodePudding user response:

I believe the problem is with yours binding configuration in your web.config. Please check security node.

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

Below you can find my working example:

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

In case if you don't use web.config configuration you can try to adjust your BasicHttpBinding creation code with:

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