简体   繁体   English

如何从IIS托管的WebAPI / MVC应用程序发出外部GET请求(通过代理服务器)

[英]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 d​​omain \\ username中添加域名前缀

This approach should work in all the 3 environments you have mentioned. 该方法应该在您提到的所有3种环境中均有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 异步 nlog 如何与托管在 IIS 中的 webapi 一起使用? - How async nlog works with webapi hosted in IIS? 如何通过IIS 7.5上托管的RESTful WCF服务从iPhone应用程序将数据输入SQL数据库 - How to enter data into SQL database from an iPhone app Via RESTful WCF service hosted on IIS 7.5 来自Azure应用服务上托管的.NET WebAPI中外部数据库驱动程序(1)的意外错误 - Unexpected error from external database driver (1) in .NET WebAPI hosted on Azure app services Azure 代理,CORs 请求时出错 IIS 托管 Z8A5DA52ED126447AD359E70C0572 - Azure Proxy, CORs error when request IIS hosted api 如何从IIS中托管的ASP应用程序执行Azure Powershell cmdlet - How Execute Azure Powershell cmdlets from a ASP app hosted in IIS 通过 WebAPI 从外部网站提取数据 C# ASP.Net MVC - Extract data from external website via WebAPI C# ASP.Net MVC 如何获取 IIS 中托管的任何站点的服务器名称和托管站点名称? - How to get the server name and hosted site name of any site hosted in IIS? 从外部MVC应用程序中的CrmOrganizationServiceContext获取OrganizationServiceProxy - Get OrganizationServiceProxy from CrmOrganizationServiceContext in external MVC app 通过Asp.Net MVC应用程序中托管的SignalR集线器从外部服务向Web客户端发送通知 - Send notification to web client from external service via SignalR hub hosted in Asp.Net MVC Application 通过带有 Windows 身份验证的 IIS 托管 mvc 应用程序,但我得到 IIS APPPOOL\\ APP 我需要连接的 Windows 用户(与 IIS express 一起使用) - Hosting a mvc app via IIS with windows authentication, but I get IIS APPPOOL\ APP I need the windows user that connects (works with IIS express)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM