Asp.net core 2.0 PostAsync在IIS 7.5上失败

6yoyoihd  于 2023-11-20  发布在  .NET
关注(0)|答案(2)|浏览(139)

在Visual Studio 2017 RC上运行时一切正常,但在部署到IIS时失败。
代码,

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"];
                }
            }
        }
    }

字符串
下面是异常的详细信息,

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()


你知道吗?

xzlaal3s

xzlaal3s1#

将应用程序池标识从ApplicationPoolIdentity更改为NetworkService解决了问题。
任何专业知识可以解释为什么解决了这个问题?

3okqufwl

3okqufwl2#

Microsoft文档中定义的应用程序池本质上是一个Windows帐户,每个可能的身份池都具有不同的权限。ApplicationPoolIdentity是权限最低的帐户,因此不具有访问网络资源的Windows权限。
另一方面,如文档所示,NetworkService可以访问网络资源。请注意,还有另一个身份池,它比NetworkService拥有更多的权限,即本地系统。
Microsoft Documentation中所述,

相关问题