2016-07-15 79 views
0

所以前一段時間我創建了一個web API來記錄誰在使用我們所有的應用程序。獲取未經授權:訪問被拒絕的網頁api

在開發中,它工作正常。它刺激,它工作了一個月,所有突然停止工作。就我所知,在prod網站上沒有任何變化。

如果我直接點擊webapi,它的工作原理。這是當我使用調用api的站點時,發生問題。

我檢查過,dev和prod之間的IIS設置是相同的。 (這兩個Windows身份驗證和它的高級設置都是相同的,匿名關閉。)

這兩個站點都使用相同的應用程序池(客戶端站點和api站點)。以域帳戶爲標識。

我已經完成了web.config和applicationhost.config的dev和prod之間的文件比較,沒有什麼看起來與衆不同。

我用盡了什麼檢查的想法。

的Web API控制器

[Authorize] 
    public class ValuesController : ApiController 
    { 
     public string Get(string samAccountName, bool success, string permissionName) 
     { 
      var returnValue = "Failed"; 

      if (!string.IsNullOrWhiteSpace(samAccountName) && !string.IsNullOrWhiteSpace(permissionName)) 
      { 
        // Do my logging (removed all the try catch etc to simplify thing) 

        returnValue = "Success"; 
      } 

      return returnValue; 
     } 
    } 

代碼打API客戶端上的

/// <summary> 
     /// This method calls a web api get method to log the call of the audit 
     /// </summary> 
     /// <param name="authorityName">This is the name of the permission to check</param> 
     /// <param name="success">This is the flag to determine if the call was successful.</param> 
     /// <returns>Returns an Async task</returns> 
     private async Task WriteToAuditLogAsync(string authorityName, bool success) 
     { 
       try 
       { 
        using (var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true })) 
        { 
         client.BaseAddress = new Uri(GlobalConfiguration.AuditBaseAddress); 
         client.DefaultRequestHeaders.Accept.Clear(); 
         client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

         var values = string.Format(
          CultureInfo.InvariantCulture, GlobalConfiguration.AuditApivalue, 
          this.userName.ToUpper(CultureInfo.InvariantCulture), 
          success.ToString(), 
          authorityName.ToUpper(CultureInfo.InvariantCulture)); 

         // HTTP GET 
         var response = await client.GetAsync(values, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); 

        } 
       } 
       catch (Exception ex) 
       { 
        LogWriter.CriticalError(ex); 
       } 
     } 

錯誤消息

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/> 
<title>401 - Unauthorized: Access is denied due to invalid credentials.</title> 
<style type="text/css"> 
<!-- 
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;} 
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;} 
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF; 
background-color:#555555;} 
#content{margin:0 0 0 2%;position:relative;} 
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;} 
--> 
</style> 
</head> 
<body> 
<div id="header"><h1>Server Error</h1></div> 
<div id="content"> 
<div class="content-container"><fieldset> 
    <h2>401 - Unauthorized: Access is denied due to invalid credentials.</h2> 
    <h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3> 
</fieldset></div> 
</div> 
</body> 
</html> 

這裏是web.config中的部分

<appSettings> 
    <add key="webpages:Version" value="3.0.0.0" /> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5.2" /> 
    <httpRuntime targetFramework="4.5.2" /> 
    <customErrors mode="Off" /> 
    <authentication mode="Windows" /> 
    </system.web> 
    <system.webServer> 
    <security> 
     <authorization> 
     <clear /> 
     <add accessType="Allow" users="*" /> 
     </authorization> 
    </security> 
    <handlers> 
     <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
     <remove name="OPTIONSVerbHandler" /> 
     <remove name="TRACEVerbHandler" /> 
     <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 
    </handlers> 
    </system.webServer> 

開發IIS日誌

2016-07-15 14:26:45 10.9.9.9 GET /ApmApi/api/Values samAccountName=somename&success=True&permissionName=somePermission 80 - 10.9.9.9- - 401 2 5 593 
2016-07-15 14:26:53 10.9.9.9 GET /ApmApi/api/Values samAccountName=somename&success=True&permissionName=somePermission 80 Domain\AppPool 10.9.9.9- - 200 0 0 7015 

PROD IIS日誌 - 好像被髮送的權限..

2016-07-15 17:09:45 10.9.9.8 GET /ApmApi/api/Values samAccountName=somename&success=True&permissionName=somePermission 80 - 10.9.9.8 - - 401 2 5 546 
2016-07-15 17:09:45 10.9.9.8 GET /ApmApi/api/Values samAccountName=somename&success=True&permissionName=somePermission 80 - 10.9.9.8 - - 401 1 3221225581 0 
+0

如果調用站點和API站點位於同一臺服務器上,則可能會遇到IIS環回阻止。請參閱http://serverfault.com/questions/485006/why-cant-i-log-in-to-a-windows-protected-iis-7-5-directory-on-the-server – howcheng

+0

謝謝,如何。我會與ops團隊一起研究這一點。我還會研究ClientCertificateOption.Automatic和httpHandler – Lareau

+0

由於某種原因,這個特定的錯誤消息在離開時被另一個替換(我將發佈一個關於該問題的新問題)。 我學到了什麼,當我碰到負載均衡的站點時,我會得到上面的錯誤。當我直接點擊該網站時,我得到了404 ..嘆氣 – Lareau

回答

0

確保您具有Integrated作爲您的應用程序池託管管道模式。

相關問題