HttpClient 401未经授权的异常'系统无法联系域控制器来为身份验证请求提供服务。

时间:2017-12-11 07:21:33

标签: c# httpclient kerberos windows-authentication ntlm

尝试使用HttpClient连接到远程ASP.NET Web API时出现异常401 Unauthorized错误:

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized. ---> System.ComponentModel.Win32Exception: The system cannot contact a domain controller to service the authentication request. Please try again later
at System.Net.NTAuthentication.GetOutgoingBlob(Byte[] incomingBlob, Boolean throwOnError, SecurityStatus& statusCode)
at System.Net.NTAuthentication.GetOutgoingBlob(String incomingBlob)
at System.Net.NegotiateClient.DoAuthenticate(String challenge, WebRequest webRequest, ICredentials credentials, Boolean preAuthenticate)
at System.Net.NegotiateClient.Authenticate(String challenge, WebRequest webRequest, ICredentials credentials)
at System.Net.AuthenticationManagerDefault.Authenticate(String challenge, WebRequest request, ICredentials credentials)
at System.Net.AuthenticationState.AttemptAuthenticate(HttpWebRequest httpWebRequest, ICredentials authInfo)
at System.Net.HttpWebRequest.CheckResubmitForAuth()
at System.Net.HttpWebRequest.CheckResubmit(Exception& e, Boolean& disableUpload)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at eNett.Contracts.Client.ClientApiProxy.<GetUsers>b__26_0(Task`1 response) in C:\eNett\Git\sl-ct\Client\eNett.Contracts.Client\ClientApiProxy.cs:line 361
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()

我有以下设置:

  1. 服务器A上托管的ASP.NET Web API
  2. HttpListener listener = (HttpListener)app.Properties["System.Net.HttpListener"];
                listener.AuthenticationSchemes = 
    AuthenticationSchemes.IntegratedWindowsAuthentication;
    
    1. ASP.NET Windows服务托管在连接到Web API的服务器A / localhost上:
    2.      var httpClient = new HttpClient(new HttpClientHandler() 
           { 
           UseDefaultCredentials = true 
           }) 
           { 
              BaseAddress = new Uri(baseUri) 
           };
           var request = new HttpRequestMessage
           {
                  Method = HttpMethod.Get,
                  RequestUri = BuildUri(relativeUri, parameters),
           };
          httpClient.SendAsync(request, cancellationToken);
      

      身份验证在以下情况下有效:

      • 我在本地PC上托管网络API并使用本地客户端

      • 进行呼叫
      • 服务器A上的web api是从我本地PC上的Web浏览器调用的

      服务器A和我的本地PC正在使用:

      • 同域
      • 与web api和客户端相同的AD凭据
      • HTTPS / SSL连接

      到目前为止我尝试过很多东西,但任何建议都会很棒。

      更新:发现环回检查是问题https://blogs.technet.microsoft.com/scottstewart/2014/09/15/disableloopbackcheck-when-routing-through-a-load-balancer-powershell-sample-included/ 通过服务器A的FQDN请求导致错误,但使用其IP工作。考虑为服务器A设置SPN以解决此问题。

1 个答案:

答案 0 :(得分:1)

发现环回检查是问题https://blogs.technet.microsoft.com/scottstewart/2014/09/15/disableloopbackcheck-when-routing-through-a-load-balancer-powershell-sample-included/通过服务器A的FQDN请求导致错误,但使用其IP工作。考虑为服务器A设置SPN以解决此问题。