1

在將我們的身份驗證機制更改爲Windows身份驗證後,我在服務調用期間收到異常。由於無法診斷,我有點失去意志。這似乎是一個非常普遍的錯誤埋在MVC身份驗證的深處。如何調試此Windows驗證問題?

的錯誤信息是:

Response status code does not indicate success: 500 (Internal Server Error) 

我的調用堆棧是這樣的(我的方法調用刪除隱私):

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +137 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +65 
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +97 
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +17 
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10 
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48 
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50 
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +225 
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +225 
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10 
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10 
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48 
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34 
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26 
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100 
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10 
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13 
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29 
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36 
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12 
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22 
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26 
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21 
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29 
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28 
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 
System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +137 

這是我的web.config:

<authentication mode="Windows" /> 
<authorization> 
    <deny users="?" /> 
</authorization> 

這裏是我使用的代碼:

HttpClientHandler handler = new HttpClientHandler() 
{ 
    UseDefaultCredentials = true 
}; 

HttpClient client = new HttpClient(handler); 
var result = await client.GetAsync(uri); 
result.EnsureSuccessStatusCode(); 

EnsureSuccessStatusCode因爲通話返回500而引發。

它沒有打我的任何代碼。我在另一端的入口處有斷點。那麼是什麼原因造成的呢?我如何調試並獲取足夠的信息來修復它?我覺得必須有辦法獲得更多我不知道的信息?

如果有人能告訴我如何診斷問題,我不一定需要解決方案嗎?

+0

當您針對Visual Studio內置的Web主機運行時,您是否遇到500錯誤,或者您是否在針對完全安裝的IIS版本運行時看到這些錯誤? –

+0

使用內置的Web主機運行。在我編寫的一個原型應用程序中,所有事情都以這種方式工作,並且令人遺憾地轉向完整的IIS不是一種選擇,因爲它是我們在公司工作的方式 – Jon

回答

0

這裏是this答案的解決方案:

if (!result.IsSuccessStatusCode) 
{ 
    string msg = result.Content.ReadAsStringAsync().Result; 
    throw new Exception(msg); 
} 

result.EnsureSuccessStatusCode(); 

很顯然,我不會推薦這對生產代碼(或作爲一種使用異步代碼),但是這將讓你在調試確切的錯誤信息你服務電話。

事實上,在實際獲取錯誤消息的五分鐘內調試錯誤並進行了修復。