Search code examples
wcf.net-coreservice

The authentication header received from the server.. BASIC - WFC - NET CORE


i working with WCF (connected service) with .NET CORE, and i want call to wfc service and send xml... but when execute service, this throw the next exception

"The authentication header received from the server was 'Basic realm="AXIS"'."

when I run the service in the SOAP UI, I assign the basic authorization option and it works perfect ... but in this part I don't know how to set the type of authorization

I also don't have any configuration xml file, just a json called ConnectedService.json

CODE:

var endpoint = new TestService.WebTest_Service.EndpointConfiguration();
var service = new TestService.WebTest_Service(endpoint);

string strXml = _getXml();
TestService.XML xml = new TestService.XML();
xml.document = strXml;

service.ClientCredentials.UserName.UserName = "username";
service.ClientCredentials.UserName.Password = "password";

var result = service.invokeAsync(xml); // => THROW ERROR

Solution

  • First, make sure the account name and password are correct, you also can use the domain/user.

    client.ClientCredentials.UserName.UserName = @"domain\username";
    client.ClientCredentials.UserName.Password  = "password";
    

    Secondly, please check the automatically generated service endpoint. It should be located in the Reference.cs. The parameter in the constructor is an enum, which indicates which endpoint should be used.

    ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(ServiceReference1.Service1Client.EndpointConfiguration.BasicHttpBinding_IService1);
    client.ClientCredentials.UserName.UserName = "administrator";
    client.ClientCredentials.UserName.Password = "abcd1234!";
    

    At last, we could also manually add Basic Http header in the soap request.

    client.ClientCredentials.UserName.UserName = "UserName";
    client.ClientCredentials.UserName.Password = "Password";
    
    using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
    {
       HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
       httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(client.ClientCredentials.UserName.UserName + ":" + client.ClientCredentials.UserName.Password));
       OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
                       
       // below is a sample call
       int response = client.addNumbers(1, 2);
       Console.WriteLine(response);
       Console.ReadLine();                    
    }
    

    Feel free to let me know if the problem still exists.