C#.NET reports "The request failed with HTTP status 401: Unauthorized" when accessing

We've recently seen an issue where C#.NET reports "The request failed with HTTP status 401: Unauthorized" when accessing a classic SOAP Web Reference.

The credentials are set using  CredentialCache.DefaultCredentials as shown in the code below.




/// <summary>
/// Starts the service.
/// </summary>
/// <remarks>The scheduler web service is not polled at start-up to improve start response times.</remarks>
public void StartService()
{
    try
    {
        server = new XIAAutomation();
        server.Credentials = CredentialCache.DefaultCredentials;
        server.Url = GetWebServiceUrl();
        server.Timeout = 60000;
        SetupTimer();
        Diagnostics.WriteEvent(String.Format(Resources.Service.StartupComplete, Assembly.GetExecutingAssembly().GetName().Version, server.Url), EventLogIdentifiers.Startup);
    }
    catch (Exception ex) { Diagnostics.WriteEvent(Resources.Service.StartupFailedException, EventLogIdentifiers.Startup, ex); }
}



 
The solution to this problem is to ensure that  the credentials are set after the URL


       
server.Url = GetWebServiceUrl();server.Credentials = CredentialCache.DefaultCredentials;


Comments

Popular posts from this blog

Windows Server 2016, 2019, 2022, Windows 10 and Windows 11: Date and time "Some settings are managed by your organization".

TFTPD32 or TFTPD64 reports Bind error 10013 An attempt was made to access a socket in a way forbidden by its access permissions.

Windows Server 2019 desktop icons such as My Computer, Windows cannot access the specified device, path, or file.