How to use C# Web Service Client with BasicAuth (for SAP)

SAP Webservice with .NET Webservice Client and BasicAuthNo matter how frumpy SAP may look most of the time, nevertheless you can build such modern things like web services inside the SAP system. Even the WSDL (Web Service Description File) file can be generated in SAP. I read about this feature (WSDL generation) just at the right time, because recent days I should write a web service in SAP which should be triggered by a C# application.

In .NET web services can easily integrated via their WSDL file by using the WebReference feature of Visual Studio. Therefore Visual Studio generates a so-called proxy class from the given WSDL file which can be used to call all functions of the webservice without thinking too much.

As far as in theory. In practice I had to choose the security mode for the web service in SAP. This mode affects how a user/client/consumer has to authenticate himself against the web service. For my web service I had choosen BasicAuth as security mode. (BasicAuth is a simple authentification model which is based on username and password.)

Unfortunately Visual Studio refuses to do a good job when generating the proxy class from SAP’s WSDL file. Namely it generates the proxy class but however it deranges the authentification code. So if you create an object of the proxy class and try to call any of the web service’s methods you will get an error message like this one:

“The HTTP request is unauthorized with client authentication scheme ‘Anonymous’. The authentication header received from the server was ‘Basic realm=”SAP Web Application Server XYZ”‘.”

SAPwebservice.YS_TEST_SERVICE sapClient = new SAPwebservice.YS_TEST_SERVICE();

sapClient.ClientCredentials.UserName.UserName = "my_username";
sapClient.ClientCredentials.UserName.Password = "my_password";

int result = sapClient.TestMethod("1234", "My test data.");

And I even had passed my username and my password in foresight. So how to fix this?

No products found.

In order to “teach” the generated proxy class to use BasicAuth with credentials in the right way to communicate with the SAP web service, it is necessary to pass the following parameters to the constructor of the proxy class.

using System.ServiceModel;
[...]

//Manually reconfigure the binding for SAP-BasicAuth
BasicHttpBinding basicAuthBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
basicAuthBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
EndpointAddress basicAuthEndpoint = new EndpointAddress("http://my_sap_server:mein_port/sap/bc/srt/rfc/sap/my_webservice?sap-client=my_client&wsdl=1.1");

SAPwebservice.YS_TEST_SERVICE sapClient = new SAPwebservice.YS_TEST_SERVICE(basicAuthBinding, basicAuthEndpoint);

If you create the binding by hand, you can specify the authentication method. In this way you can persuade the proxy class to use the credentials (my_username and my_password) we set up in the code above.

That’s the way to use SAP web services in combination with C#/.NET clients.

12 Comments

  1. Unfortunately described method does not help to call SAP CC web services where wsse:Security is used.

  2. Marlos Damascenosays:

    Hi there,

    Try to look at your App.config or Web.Config and look for:

    There you may change your configs to:

    That will solve the issue!!!
    Do not use the standard

  3. Adrian Di Ruggierosays:

    Hello,
    Thank you for your post. It was very useful!

  4. SAP_Avengersays:

    Hi my newbie question. My SAP service does not have the options for basicAuthBinding, basicAuthEndpoint like you list below.

    SAPwebservice.YS_TEST_SERVICE sapClient = new SAPwebservice.YS_TEST_SERVICE(basicAuthBinding, basicAuthEndpoint);

    How can I require basic auth in the app.config file of a WCF client instead. My app config file would appear as:

    http://mysapserver/sap/bc/srt/rfc/sap/mySapActions/010/zss_fmySapActions//dosomethig

  5. I had same trouble. Now, Fixed. Thank you !!!

  6. Germánsays:

    Thank you!
    I had found other examples but using the method of VS2005 (Web References instead of Services Reference). Thus, the current available technology is used.

    By the way, my English is generated by Google translator. sorry if poorly written

  7. Michael Schönbauersays:

    Hi,

    thanks for your entry, it really helped me out a lot, i can finally consume from a SAP WebService – however, i still have some Problems to get the Responses to work (they are always null) – maybe a Namespace issue – i created the Proxy directly from the wsdl file, which is Soap 1.2 …

    greetings, Mike

  8. Francesco Palmisanosays:

    I was in trouble two days ago, but your article has saved me! I had the specified error you have published and I had a different working code with another project on Sap past, but did not work with the last. I used your lines of code I have resolved, if you had not published, perhaps I would have spent a lot of time to understand the problem.

    Thank you very much.

  9. SN95says:

    This is awesome, I had no idea C# could interact with SAP. Thanks for sharing! I’m going to go play around with the web services and see what kind of cool applications I can create for this.

    • Miguelsays:

      This ist good, but…

      Ich have received a wsdl file from SAP without security information. I am following this article but i don´t know what are your doing with “basicAuthBinding”, and “basicAuthEndpoint”.

      Thanks.

Leave a comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked