Search code examples
azurewebrequest

webrequest.getrequeststream throws exception when run as "nt_authority\" system


I am trying to get OAuth2 token from Azure. I am using HttpWebRequest.GetRequestStream for the token request. When I run my application from Visual studio or by double clicking it, it works fine. But when I use this code from my product, it is actually a service that spawns this application. So, it is running as local system account. Then it throws the following exception: The underlying connection was closed: An unexpected error occurred on a send. Inner exception is: Authentication failed because the remote party has closed the transport stream

Note: my target .Net framework is 4.6.1 And i am using ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; in my code.

Any help will be appreciated.


Solution

  • Found the answer. The error was totally misleading; at least to me. Since proxy server address was not set for the local system account, it wasn't working before. Silly me :)