2013-09-30 133 views
7

我在這個問題上扯出了我的頭髮,我有一個WCF服務,我可以通過瀏覽器調用它並且工作正常,當我從Web應用程序調用它時下面的方法我得到一個(401)未授權的錯誤。該服務不會被調用。更重要的是,當我從我的本地機器(使用IIS Express的調試模式)運行我的Web應用程序時,它指向我的開發服務器(IIS7),但它工作正常,但是當我將我的Web應用程序部署到開發服務器並指向開發人員服務器時401錯誤失敗。我認爲這與IIS7有關,但我不是100%肯定的,並且幫助會非常有用。沒有將憑據傳遞給WCF服務導致401

我已經在線尋找答案,但迄今爲止我找到的最好的是this

我的服務電話如下:

var request = (HttpWebRequest) WebRequest.Create(url); 
request.Method = "GET"; 
request.ContentType = "application/json; charset=utf-8"; 
request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested; 
request.Credentials = CredentialCache.DefaultCredentials; 

WebResponse responce = request.GetResponse(); 
Stream reader = responce.GetResponseStream(); 

var sReader = new StreamReader(reader); 
string outResult = sReader.ReadToEnd(); 
sReader.Close(); 

var result = (T) JsonConvert.DeserializeObject(outResult, typeof (T)); 
return result; 

我的服務配置是這樣的:

<service name="RGMPServices.Householding.Services.AccountService" behaviorConfiguration="Default"> 
    <endpoint address="" kind="webHttpEndpoint" endpointConfiguration="SecuredHttpEndpointBinding" contract="RGMPServices.Householding.Contracts.IAccountService" /> 
    </service> 

    <service name="RGMPServices.Householding.Services.HouseholdService" behaviorConfiguration="Default"> 
    <endpoint address="" kind="webHttpEndpoint" endpointConfiguration="SecuredHttpEndpointBinding" contract="RGMPServices.Householding.Contracts.IHouseholdService" /> 
    </service> 

    <service name="RGMPServices.Householding.Services.UserService" behaviorConfiguration="Default"> 
    <endpoint address="" kind="webHttpEndpoint" endpointConfiguration="SecuredHttpEndpointBinding" contract="RGMPServices.Householding.Contracts.IUserService" /> 
    </service> 
</services> 

<behaviors> 
    <endpointBehaviors> 
    <behavior name="webBehaviour"> 
     <webHttp /> 
    </behavior> 
    </endpointBehaviors> 
    <serviceBehaviors> 
    <behavior name="Default"> 
     <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 

<standardEndpoints> 
    <webHttpEndpoint> 
    <standardEndpoint name="SecuredHttpEndpointBinding" helpEnabled="true" automaticFormatSelectionEnabled="true"> 
     <security mode="TransportCredentialOnly"> 
     <transport clientCredentialType="Windows" /> 
     </security> 
    </standardEndpoint> 
    </webHttpEndpoint> 
</standardEndpoints> 

我已經把一些日誌記錄在客戶服務電話,就在我給該服務打電話,迴應是:

DEBUG 2013-10-01 13:15:13,569 452ms ServiceGetSingle - Passing Login: MYLANDOMAIN\MYLANUSERNAME

ERROR 2013-10-01 13:15:13,631 514ms ServiceGetSingle - ERROR Calling ServiceGetSingle with user credentials login: MYLANDOMAIN\MYLANUSERNAME System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse() at Householding.Common.ServiceHelper.ServiceGetSingle[T](String url)

代碼如下:

logger.Debug("Passing Login: " 
    + System.Security.Principal.WindowsIdentity.GetCurrent().Name) 

即使當我設置的應用程序池爲我的網站我的域帳戶現在還沒有授權我訪問WCF服務,但再次:它的工作對瀏覽器。太奇怪了!

+0

獲取WCF和Windows身份驗證並不總是很容易。試試客戶端上的Fiddler來跟蹤http流。此外,請嘗試在服務器上配置WCF跟蹤:http://msdn.microsoft.com/en-us/library/ms733025.aspx – Joe

回答

1

使用Integrated Windows Authentication(IWA)和Kerberos時,您似乎很可能是雙跳問題的受害者。第一跳從您的瀏覽器到Web應用程序;第二跳從您的Web應用程序到WCF服務。

這裏有一些資源更充分的說明問題,並且可以提供一個解決方案:

您可以配置Active Directory以支持Kerberos delegation (通常基礎設施人員不喜歡這樣),或者你可以關閉模​​擬和我們e Web應用程序和IIS應用程序池的「服務」帳戶,可以代表最終用戶使用WCF服務進行身份驗證。

1

Dev服務器上的默認憑據是什麼?試着在那裏做一個日誌,看看你得到了什麼。

這是我懷疑:在本地運行,證書是你的Windows信譽。當你從開發者調用開發服務器時,憑證將是網站運行的任何帳戶。如果該特定帳戶無法訪問,那麼它會炸燬。

+0

已更新問題 – Joshy

+0

從您從開發服務器運行應用程序的日誌? –

+0

是的,它來自開發服務器 – Joshy

1

他們之前說過,這看起來像是一個模仿問題。 您是否嘗試用「運行方式」啓動客戶端程序來更改憑據?

此外,您可以在此行代碼

request.Credentials = CredentialCache.DefaultCredentials; 

改變

request.Credentials = new NetworkCredential("MyUsername", "MyPassword"); 

,看看它是否工作。您還需要在Web服務器上使用「MyPassword」創建帳戶「MyUserName」,以使其工作。

1

當經過身份驗證的用戶無權訪問託管WCF服務的物理路徑時,可能會導致這些錯誤。在開發服務器上,打開IIS管理器並導航到該服務的虛擬目錄。在操作欄右側,點擊「基本設置」。在「物理路徑」文本框下方,點擊「連接爲...」。選擇「特定用戶」並嘗試將其設置爲您知道對開發服務器上的物理文件夾擁有權限的用戶帳戶。通常,這將是一個服務帳戶,其密碼不會過期。

1

從瀏覽器運行時,瀏覽器正在發送您的身份驗證憑據。此外,iis express將以登錄用戶身份運行,所以這也會發送您的憑據。 Iis不同,它將作爲本地帳戶運行。即使您在前端iis上進行身份驗證,也不會將其傳遞給後端。 Windows模擬令牌的允許跳數是有限的,通常爲0.這是爲了防止你正在做的事情。 如果您希望前端身份驗證流向後端,那麼您應該自己進行身份驗證,並抓取用戶/通行證。或者,如果您自己進行身份驗證,您可以創建一個模擬令牌,允許跳轉到另一臺計算機並且它應該可以工作。