[英]How to make an external GET request from IIS hosted WebAPI/MVC app (via proxy server)
We use Windows authentication . 我们使用Windows身份验证 。 I've created both WebAPI / MVC applications to play with.
我已经创建了两个WebAPI / MVC应用程序。
When hosted on production server / IIS appplication requires credentials to perform HTTP GET to an external site. 当托管在生产服务器 / IIS上时,应用程序需要凭据才能对外部站点执行HTTP GET 。 For some reasons we temporarily cannot use special AD account for that.
由于某些原因,我们暂时无法使用特殊的AD帐户 。
I've read about ASP.NET impersonation . 我已经阅读过有关ASP.NET模拟的信息 。 As far as I understand it is possible to incorporate with caller's credentials?
据我了解,可以合并呼叫者的凭证吗? I've tried to use this code though I'm not sure whether it is an appropriate approach to use with WebAPI .
尽管我不确定这是否适合与WebAPI一起使用,但我尝试使用此代码。
[HttpGet]
[Route("api/test")]
public HttpResponseMessage test() {
using(var ctx = WindowsIdentity.GetCurrent().Impersonate()) {
var proxyOptions = new WebProxy("[proxyUrl]");
proxyOptions.UseDefaultCredentials = true;
var client = new WebClient();
client.UseDefaultCredentials = true;
client.Proxy = proxyOptions;
return new HttpResponseMessage {
Content = new StringContent(
content: client.DownloadString("[externalApiUrl]"),
encoding: Encoding.Default,
mediaType: "text/plain"
)
};
}
}
Anyway this doesn't work. 无论如何,这是行不通的。
Should I configure delegation instead of impersonation ? 我应该配置委派而不是模拟吗?
How to perform an external request without a separate AD account for that? 如何在没有单独的AD帐户的情况下执行外部请求?
Have you tried explicitly setting the credentials using 您是否尝试使用以下方式显式设置凭据
var proxyConfig = new WebProxy(
proxyServerUrl,
BypassOnLocal: false,
BypassList: null,
Credentials: new NetworkCredential(username, password)
);
The username / password should be an authorized user on your proxy server. 用户名/密码应该是代理服务器上的授权用户。 Since you are using Windows Authentication, you may need to prefix domain name in the username Eg domain\\username
由于您使用的是Windows身份验证,因此可能需要在用户名Eg domain \\ username中添加域名前缀
This approach should work in all the 3 environments you have mentioned. 该方法应该在您提到的所有3种环境中均有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.