azure - 将 AuthenticationContext 和 ActiveDirectoryClient 与应用程序代理一起使用?

标签 azure azure-active-directory adal azure-ad-graph-api

使用 Azure Active Directory 图形客户端 API,如何配置底层 HttpClient 以使用 HttpClientHander,在其中我可以定义经过身份验证的应用程序代理?

var proxy = new WebProxy(...);
proxy.Credentials = ...;
var handler = new HttpClientHandler { Proxy = proxy, UseProxy = true};
var auth = new AuthenticationContext(...);
var client = new ActiveDirectoryClient(...);

或者,我可以不使用代理后面的图形客户端吗?

谢谢

最佳答案

我正在探索同样的问题。经过一番挖掘,但我找到了解决方案。现在,我意识到您具体询问了如何申请 HttpClientHandler 。我不知道这是否可以做到;但是,您可以应用代理。方法如下。

ActiveDirectoryClient类提供了 DataServiceContextWrapper属性名为 Context ,毫不奇怪,它是 DataServiceContext 的包装。 .

这很好。它将问题简化为弄清楚如何将代理应用到 DataServiceContext类(class)。我使用了一些我手边的旧代码,结果几乎爆炸了。这是因为我使用了已弃用的 SendingRequest事件来拦截请求并在请求发出之前应用代理。此客户端与已弃用的事件不兼容。

花了更多的时间来弄清楚如何使用 SendingRequest2 来做到这一点。事件;它只需要一点点类型转换。

这样做:

var client = new ActiveDirectoryClient(...);
client.Context.SendingRequest2 += OnSendingRequest2;

// ...

static void OnSendingRequest2(object sender, SendingRequest2EventArgse)
{
    var request = ((HttpWebRequestMessage)e.RequestMessage).HttpWebRequest;
    request.Proxy = new WebProxy("http://myproxy:port");
}

不要这样做:(它已被弃用,并且会产生异常。)

var client = new ActiveDirectoryClient(...);
client.Context.SendingRequest += OnSendingRequest;

// ...

static void OnSendingRequest(object sender, SendingRequestEventArgs e)
{
    e.Request.Proxy = new WebProxy("http://myproxy:port");
}

关于azure - 将 AuthenticationContext 和 ActiveDirectoryClient 与应用程序代理一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36802585/

相关文章:

Azure 数据工厂从 foreach 值获取数据

azure-active-directory - 通过 Microsoft Graph 检查目录邀请的兑换状态

azure - 如何从应用程序(客户端)ID 和目录(租户)ID 获取对象 ID?

ios - ADALiOS AcquireToken()

angular - 如何使用 Azure AD 作为身份提供者为 Angular 7.x 应用程序实现基于 SAML 的身份验证?

c# - ASP.NET 中的安全设置问题

powershell - Azure 地理复制数据库和数据库层

具有 Office 365 身份验证的 Spring Boot 应用程序

c# - 找不到方法 : AcquireToken(System. 字符串,Microsoft.IdentityModel.Clients.ActiveDirectory.ClientAssertionCertificate)

asp.net-core - Microsoft.AspNetCore 与 Microsoft.IdentityModel (ADAL) 与 Azure AD 中的 .net 核心 API