Windows service HttpClient with HTTPS support

39662036 0 Reputation points
2024-08-20T15:23:18.29+00:00
Hi.I have a **Windows service** that makes calls to an http web page via an **HttpClient **object.  Now the website is HTTPS but when I call the https page it gives an "Unauthorized" error. I am using Visual Studio 2013 with .NET Framework 4.5.
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,500 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,995 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
341 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Asha Kanta Sharma 456 Reputation points
    2024-08-20T15:54:01.6666667+00:00

    Ensure that your .NET application is configured to use the appropriate version of TLS. .NET Framework 4.5 supports TLS 1.0 by default, but if the server requires TLS 1.2, you need to enable it. Ensure the SSL certificate on the server is valid and trusted. If the certificate is self-signed or not in the trusted root store, you might encounter issues. If the server requires authentication, ensure you provide the correct credentials. If the server uses token-based authentication (e.g., Bearer tokens), ensure you include the appropriate authorization header.Ensure that the Windows service is running under an account with the necessary permissions to make network requests. If running under a local system account, it may have restrictions that affect HTTPS requests.

    0 comments No comments

  2. youzeliang Suspended 735 Reputation points
    2024-08-20T15:54:11.0066667+00:00

    Certificate Issues: If the site uses a self-signed or untrusted certificate, you may need to handle these certificates in your application. You can bypass certificate validation by configuring the ServerCertificateCustomValidationCallback in HttpClientHandler (only for testing purposes, not recommended in production).

    so, you can check it

    and you can enforce a newer TLS version by setting the ServicePointManager.SecurityProtocolServicePointManager.SecurityProtocol:

    i hope you can accept my answer

    0 comments No comments

  3. Bruce (SqlWork.com) 66,056 Reputation points
    2024-08-20T20:29:05.5933333+00:00

    the 401 error means the request failed to send the proper authorization with the request. you will need to find out what the url requires. common options are:

    • site uses basic authenication
    • site uses ntlm/kerberos authentication
    • site uses cookie authentication. this would require call a login url, and getting a cookie that is sent with the request.
    • site uses bearer tokens. this requires calling an oauth server to get a token passed with the bearer header
    • site ssl certificate. ssl supports the client passing a certificate to create the connection. but this typically causes a ssl connection error, not 401
    0 comments No comments

  4. Lan Huang-MSFT 29,666 Reputation points Microsoft Vendor
    2024-08-21T06:38:49.54+00:00

    Hi @39662036,

    If you receive an access denied when calling a Web service with anonymous authentication turned off, you can use the following method.

    Resolution 1: Assign DefaultCredentials to Credentials property

    Resolution 2: Use the CredentialCache class

    If http works, but https doesn't, you need to check your TLS version.

    The default version of the .NET Framework 4.5 is TLSv1. However, TLS 1.0 and 1.1 have been deprecated. So you have to explicitly set the security protocol, rather than having .NET or the operating system choose the security protocol.

    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    For more information, please visit:

    Transport Layer Security (TLS) best practices with .NET Framework

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.