vml19
vml19

Reputation: 3864

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?

Upvotes: 0

Views: 482

Answers (2)

Rafael Vargas
Rafael Vargas

Reputation: 11

The Application Pool, as defined in Microsoft documentation, is essentially a Windows account, and each possible Identity Pool has different privileges. The ApplicationPoolIdentity is the least privileged account and, as a result, does not have Windows rights to access network resources.

On the other hand, as the documentation indicates, the NetworkService has access to network resources. Note that there is another identity pool with more rights thant he NetworkService, that is the Local System.

As mentioned in the Microsoft Documentation

Upvotes: 1

vml19
vml19

Reputation: 3864

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

Any expertise can explain as what solved the problem?

Upvotes: 1

Related Questions