通过Web代理获取Azure访问令牌 [英] Get Azure access token through Web Proxy
问题描述
我们正在使用 Microsoft.IdentityModel.Clients.ActiveDirectory API从Azure AD获取访问令牌.
We are using Microsoft.IdentityModel.Clients.ActiveDirectory API to acquire access token from Azure AD.
我们有要求API调用应通过Web代理传递.我们找不到任何相关的示例代码.
We have the requirement API call should pass through web proxy. We couldn't find any relevant sample code.
是否有实现此要求的选项.请找到所使用的代码段.
Is there any option to achieve this requirement. Please find the code snippet used.
string accessToken = string.Empty;
AuthenticationContext authContext = new AuthenticationContext(string.Format(authority, subscription.TentantId));
var clientCredential = new ClientCredential(subscription.ClientId, subscription.SecertKey);
var result = authContext.AcquireToken(resource, clientCredential);
accessToken = result.AccessToken;
return accessToken;
推荐答案
恐怕该库不支持IWebProxy对象的设置-我找不到方法.但是,似乎有两种解决方案:
I am afraid that this library does not support setting of IWebProxy object - I couldn't find a way. However there seem to be two solutions:
1)您可以设置全局代理,但是建议您这样做.见下文:
1) You can set global proxy, however this is recommended practice. See below:
WebRequest.DefaultWebProxy = new WebProxy("10.0.0.1:1234");
2)您可以通过REST api调用请求令牌,并将代理手动设置为HttpRequest
2) You can request the token via REST api call and set proxy manually to your HttpRequest
POST "https://login.microsoftonline.com/{tenantName}/oauth2/token"
在某些情况下(jwt客户断言类型等),这可能很棘手.
this could be tricky in some cases (jwt client assertion type etc..)
请参见以下示例: 查看全文