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.
Windows service HttpClient with HTTPS support
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.
4 answers
Sort by: Most helpful
-
-
youzeliang 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
inHttpClientHandler
(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
-
Bruce (SqlWork.com) 67,251 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
-
Lan Huang-MSFT 29,911 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