Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre authenticate web service request in C#

I'm having a problem with calling a web service request in C#.

The service and request are working fine in Soap UI with the option 'Authenticate Preemptively' enabled (File, Preferences, HTTP Settings). Without this setting enabled the service returns a 'Java.Lang.NullPointerException'.

The problem I'm having is that I do not know how to enable this setting in a C# context.

I have a .NET 3.5 class library which holds a so called service reference to the specific service. This is a simple code snippet;

try
{
    CatalogService.CatalogChangeClient service = new CatalogService.CatalogChangeClient();
    service.ClientCredentials.UserName.UserName = "fancydress";
    service.ClientCredentials.UserName.Password = "47fda9cb4b51a9e";
    service.ClientCredentials.SupportInteractive = true;

    ProductUpdate[] products = new ProductUpdate[1];
    products[0] = new ProductUpdate();
    products[0].ProductCode = "00001";
    products[0].ProductDescription = "TestProduct";

    string result = service.UpdateProducts(products);
}
catch (Exception exception)
{
    Console.WriteLine(exception.Message);
}

Update after first reply.

The CatalogService.CatalogChangeClient class seems to implement the WCF abstract class

System.ServiceModel.ClientBase<TChannel>

End Update

Could anyone help me set this property?

like image 724
Rob Maas Avatar asked Aug 17 '11 07:08

Rob Maas


People also ask

How do I Authenticate Web API?

Web API assumes that authentication happens in the host. For web-hosting, the host is IIS, which uses HTTP modules for authentication. You can configure your project to use any of the authentication modules built in to IIS or ASP.NET, or write your own HTTP module to perform custom authentication.

How do I add basic authentication to WSDL?

The MustSupportBasicAuthentication element within a policy is required to enable basic authentication in the endpoint. The user name and password fields can be specified either as plain text in the WSDL, or as tokens in the WSDL and configured at runtime.

What is C# authentication?

Authentication is the process of determining a user's identity. Authorization is the process of determining whether a user has access to a resource. In ASP.NET Core, authentication is handled by the authentication service, IAuthenticationService, which is used by authentication middleware.

What is PreAuthenticate?

With the exception of the first request, the PreAuthenticate property indicates whether to send authentication information with subsequent requests without waiting to be challenged by the server.


1 Answers

You could try and override the GetWebRequest method from your generated client stub. I have used this once and that solved my problem.

Look at the following URL:

http://www.eggheadcafe.com/community/wcf/18/10056093/consuming-webservices-and-http-basic-authentication.aspx

Scroll a bit down.

Here's the code from the link:

protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
    HttpWebRequest request;
    request = (HttpWebRequest)base.GetWebRequest(uri);

    if (PreAuthenticate)
    {
        NetworkCredential networkCredentials =
            Credentials.GetCredential(uri, "Basic");

        if (networkCredentials != null)
        {
            byte[] credentialBuffer = new UTF8Encoding().GetBytes(
                networkCredentials.UserName + ":" +
                networkCredentials.Password);
            request.Headers["Authorization"] =
                "Basic " + Convert.ToBase64String(credentialBuffer);
        }
        else
        {
            throw new ApplicationException("No network credentials");
        }
    }
    return request;
}
like image 96
Nicklas Møller Jepsen Avatar answered Oct 03 '22 23:10

Nicklas Møller Jepsen