【发布时间】:2015-02-27 19:47:57
【问题描述】:
我很难让运行时模拟发挥作用。
场景:
- 在所有服务器中禁用匿名访问,并启用 Windows 身份验证
- 客户端调用 Web Api 1
- Web Api 1 可以调用 Web Api 2 或 oData 服务
- 需要使用客户端凭据模拟从 Web Api 1 到 Web Api 2 的调用
- 不得模拟从 Web Api 1 到 oData 服务的调用
- Web Api 1 使用 Web 请求调用这两个服务
- 我们已正确配置 Kerberos 委派
什么有效(有点):
如果我使用 Web.config 在 Web Api 1 中打开模拟
<authentication mode="Windows"/>
<identity impersonate="true"/>
- 所有呼叫都被模拟。 Web Api 1 --> Web Api 2 和 Web Api 1 --> oData 服务
这不是我们想要的。我们希望只能通过应用程序池帐户访问 oData 服务。因此,我们不想模拟来自 Web Api 1 的所有呼出电话。
程序化模拟
我们尝试使用以下代码仅模拟从 Web Api 1 到 Web Api 2 的调用
在 Web.config 中禁用模拟
<authentication mode="Windows"/>
<identity impersonate="false"/>
模拟从 Web Api 1 到 Web Api 2 的调用。
// Impersonate the currently authenticated User
using (((WindowsIdentity)HttpContext.Current.User.Identity).Impersonate()) {
var request = (HttpWebRequest)HttpWebRequest.Create(uri);
...
...
request.Credentials = CredentialCache.DefaultCredentials;
response = (HttpWebResponse)request.GetResponse();
};
结果:
- 未模拟从 Web Api 1 到 oData 的调用(如预期)
- 也不会模拟从 Web Api 1 到 Web api 2 的调用。这就是问题所在。
问题:
- 这是否应该在 Web 服务中实现运行时模拟?
- 我们做错了什么?
任何指针都会有所帮助。
【问题讨论】:
标签: web-services c#-4.0 kerberos impersonation kerberos-delegation