none
.net core The HTTP request is unauthorized with client authentication scheme 'Basic'. The authentication header received from the server was 'Basic Realm'. RRS feed

  • Question

  • I have an .Net CORE   console project  that consumes a WCF service using connected service (the service is for Basic authentication).  I am testing the method  but I got the below error

    The HTTP request is unauthorized with client authentication scheme 'Basic'. The authentication header received from the server was 'Basic Realm'.

    This svc  functionality working properly in  Asp.net console application project but  not working in .net core console application.

    Below is my code:

     BasicHttpBinding basicHttpBinding = null;
                EndpointAddress endpointAddress = null;
                ChannelFactory<ISalesOrderService> factory = null;
                ISalesOrderService serviceProxy = null;
                string testString = null;
                string paraMessage = "username";

                basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
                basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
                endpointAddress = new EndpointAddress("https://xxx.svc");
                factory = new ChannelFactory<ITestService>(basicHttpBinding, endpointAddress);
                factory.Credentials.UserName.UserName = "myusername";
                factory.Credentials.UserName.Password = "mypassword";
                serviceProxy = factory.CreateChannel();
                testString = "I am a test";

                // *** EXECUTE *** \\
                try
                {
                    var result =  serviceProxy.GetRecordByIdAsync("111111").Result;
                }
                catch(Exception ex) {

                    throw ex;
                }

    Friday, February 28, 2020 7:19 AM

All replies


  • Hi,
    Theoretically, it should work properly. Now that we have generated a client proxy automatically by using Microsoft WCF Web Service Reference Provider, we could call it by using the below code.
      ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
                client.ClientCredentials.UserName.UserName = "administrator";
                client.ClientCredentials.UserName.Password = "abcd1234!";
                var result = client.TestAsync();
                Console.WriteLine(result.Result);


    Please note that change the default service endpoint (localhost) to the practical service address before making a call to the service. it situated the Reference.cs file.
    Besides, the communicating with the remote Https server requires trust relationship between the client-side and the server-side. Thereby we should install the server certificate on the client-side in order to trust the server.
    Feel free to let me know if the problem still exists.
    Best Regards
    Abraham
    Friday, February 28, 2020 1:03 PM
    Moderator