我在這個問題上扯出了我的頭髮,我有一個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服務,但再次:它的工作對瀏覽器。太奇怪了!
獲取WCF和Windows身份驗證並不總是很容易。試試客戶端上的Fiddler來跟蹤http流。此外,請嘗試在服務器上配置WCF跟蹤:http://msdn.microsoft.com/en-us/library/ms733025.aspx – Joe