Search code examples
c#asp.netiishttpclient

When I run it from VisualStudio, HttpClient connects, but it does not connect from IIS


When I run it locally with VisualStudio, everything works normally, but when I put it in IIS on the server, I realized that it cannot resolve the domain because it is waiting there forever.

I don't know whether I need to grant permission from IIS or WinServer2012.

I tried adding a record to the lmhost file. It didn't work.

Note: The page opens in Chrome on the server.(https://app.xxxxxxx.com/)

My code:

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://app.xxxxxxx.com/");
request.Headers.Add("Accept", "application/json");
request.Headers.Add("Authorization", "Bearer ...........");

string contentText = $@"{{""app_id"":""{_vInfo._id}"",""app_secret"":""{_vInfo._secret}""}}";

using (HttpContent content = new StringContent(contentText, null, "application/json"))
{
    request.Content = content;
    var response = await client.SendAsync(request);  //<<<---------- Waiting here forever
    response.EnsureSuccessStatusCode();
    string _result = await response.Content.ReadAsStringAsync();
}

Event Viewer record:

Event code: 3001 
Event message: The request has been aborted. 
Event time: 15.11.2024 12:47:16 
Event time (UTC): 15.11.2024 09:47:16 
Event ID: 4d45d21891154f469c4ca24420c37d85 
Event sequence: 4513 
Event occurrence: 1 
Event detail code: 0 

Application information: 
    Application domain: /LM/W3SVC/1/ROOT-8-133761375015957277 
    Trust level: Full 
    Application Virtual Path: / 
    Application Path: C:\inetpub\wwwroot\MyWeb\ 
    Machine name: WIN22 

Process information: 
    Process ID: 7576 
    Process name: w3wp.exe 
    Account name: NT AUTHORITY\SYSTEM 

Exception information: 
    Exception type: HttpException 
    Exception message: Request timed out.



Request information: 
    Request URL: https://myweb.com:443/ConnectSvc 
    Request path: /ConnectSvc 
    User host address: xxx.xxx.xxx.xxx 
    User:  
    Is authenticated: False 
    Authentication Type:  
    Thread account name: NT AUTHORITY\SYSTEM 

Thread information: 
    Thread ID: 66 
    Thread account name: NT AUTHORITY\SYSTEM 
    Is impersonating: False 
    Stack trace: 


Custom event details: 

Solution

  • The updates I made based on a friend's answer worked. I wish he hadn't deleted the answer. I would consider it valid. Unfortunately, someone gave -1 point and he deleted the answer.

    I change it: IIS -> Application Pools -> Advanced Settings

    Identity = NetworkService
    

    and I add code base:

    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
    

    my problem is solved.