Search code examples
.netasp.net-core.net-coreiis-7.5

Asp.net core 2.0 PostAsync fails on IIS 7.5


Everything fine while running on Visual Studio 2017 RC, however it fails when deployed to IIS.

Code,

    private void ConnectApi()
    {
        var jsonCredentials = new
        {
            username = AppSettings.ApiUserName,
            password = AppSettings.ApiPassword,
            hostName = AppSettings.ApiHostName
        };

        var request = new StringContent(JsonConvert.SerializeObject(jsonCredentials), Encoding.UTF8, "application/json");           
        var url = string.Format("{0}", AppSettings.Api_BaseUrl + AppSettings.Api_Authenticate);
        using (var client = new HttpClient())
        {
            var response = client.PostAsync(new Uri(url), request).Result;
            response.EnsureSuccessStatusCode();
            if (response.IsSuccessStatusCode)
            {
                var responseBody = response.Content.ReadAsStringAsync().Result;
                string authorizationToken = string.Empty;
                if (!string.IsNullOrEmpty(responseBody))
                {
                    dynamic jsonContent = JsonConvert.DeserializeObject(responseBody);
                    AccessToken = jsonContent["authorizationToken"];
                }
            }
        }
    }

Here is the exception details,

var response = client.PostAsync(new Uri(url), request).Result;

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: Access is denied
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.Tasks.RendezvousAwaitable`1.GetResult()
   at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()
   --- End of inner exception stack trace ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()

Any idea?


Solution

  • Changing Application Pool identity from ApplicationPoolIdentity to NetworkService solved the problem.

    Any expertise can explain as what solved the problem?