gpt4 book ai didi

c# - 未将凭据传递给 WCF 服务导致 401

转载 作者:太空狗 更新时间:2023-10-29 20:27:43 25 4
gpt4 key购买 nike

我在这件事上焦头烂额,我有一个可以通过浏览器调用的 WCF 服务,它工作正常,当我使用以下方法从 Web 应用程序调用它时,我得到一个 ( 401) 未经授权 错误。并且该服务不会被调用。更重要的是,当我从我的本地机器(使用 IIS Express 的 Debug模式)指向我的开发服务器(IIS7)运行我的 Web 应用程序时,它可以工作,但是当我将我的 Web 应用程序部署到开发服务器并将其指向开发服务器服务时失败机智 401 错误。我认为这与 IIS7 有关,但我不是 100% 确定,帮助会非常有用。

我在网上寻找答案,但到目前为止我找到的最好的答案是 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)

即使我将我网站的 AppPool 设置为我的域帐户,它仍然没有授权我访问 WCF 服务,但同样:它适用于浏览器。太奇怪了!

最佳答案

使用 Integrated Windows Authentication 时,您似乎是双跳问题的受害者(IWA) 和 Kerberos .第一跳是从您的浏览器到 Web 应用程序;第二个跃点是从您的 Web 应用程序到 WCF 服务。

这里有一些资源可以更全面地解释这个问题,并可能提供解决方案:

您可以配置 Active Directory 以支持 Kerberos delegation (通常基础架构人员不喜欢这样),或者您可以关闭模​​拟并为 Web 应用程序和 IIS 应用程序池使用“服务”帐户,该帐户可以代表最终用户通过 WCF 服务进行身份验证。

关于c# - 未将凭据传递给 WCF 服务导致 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19104140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com