C# GraphSdk 的代理配置
Proxy Configuration for C# GraphSdk
我需要通过代理中继 C# Graph-Sdk 发出的 HTTP 请求。
在文档中我找不到任何关于代理设置的信息。
我目前找到的唯一解决方法是更改全局代理设置:
System.Net.GlobalProxySelection.Select = proxy;
or
System.Net.WebRequest.DefaultWebProxy = proxy;
遗憾的是,在我的情况下,如果不移动所有图表,这是不可能的
相关功能到一个单独的进程中(因为主进程的其余部分需要 运行 没有代理)。
所以我的问题是:
sdk有官方支持代理设置吗?
未来的 sdk 版本是否计划支持代理设置?
您可以在实例化 GraphServiceClient 时设置代理。
2021 年 6 月 9 日更新
现在有一个更好的方法,就是使用 GraphClientFactory。
HttpClient httpClient = GraphClientFactory.Create(GetClientCredentialProvider(), "v1.0", "Global", new WebProxy(""));
var graphServiceClient = new(httpClient);
旧答案
System.Net.Http.HttpClientHandler httpClientHandler = new System.Net.Http.HttpClientHandler()
{
AllowAutoRedirect = false,
Proxy = new WebProxy() // TODO: Set your proxy settings.
};
HttpProvider httpProvider = new HttpProvider(httpClientHandler, true);
GraphServiceClient client = new GraphServiceClient("https://graph.microsoft.com/v1.0",
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
var token = await goGetSomeTokenNow();
requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", token);
}), httpProvider);
this 文章中有 Microsoft.Graph 4+ 的新方法。然而,由于我们的 azure adfs 只有 auth 请求来自浏览器,因此我必须混合使用两种方式来实现这一点。希望这对其他人有帮助。
private GraphServiceClient GetGraphClient()
{
string[] scopes = new string[] { "User.Read", "User.ReadBasic.All", "Mail.Read", "Mail.ReadWrite", "Mail.Send" };
var msalFactory = new MsalHttpClientFactoryOwn(_configuration);
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create("")
.WithTenantId("")
.WithHttpClientFactory(msalFactory)
.Build();
UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication, scopes);
HttpClient httpClient = GraphClientFactory.Create(authProvider, "v1.0", "Global", msalFactory.GetWebProxy());
var graphClient = new GraphServiceClient(httpClient);
return graphClient;
}
public class MsalHttpClientFactoryOwn : IMsalHttpClientFactory
{
private readonly IConfiguration configuration;
public ProxiedHttpClientFactory(IConfiguration configuration)
{
this.configuration = configuration;
}
public HttpClient GetHttpClient()
{
var proxyHttpClientHandler = new HttpClientHandler()
{
UseProxy = true,
UseDefaultCredentials = false,
Credentials = GetNetworkCredentials(),
Proxy = GetWebProxy()
};
var httpClient = new HttpClient(proxyHttpClientHandler);
httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
httpClient.Timeout = TimeSpan.FromMinutes(30);
return httpClient;
}
public WebProxy GetWebProxy()
{
var proxy = new WebProxy
{
Address = new Uri("proxy address"),
BypassProxyOnLocal = false,
UseDefaultCredentials = false,
Credentials = GetNetworkCredentials()
};
return proxy;
}
private NetworkCredential GetNetworkCredentials()
{
var networkCreds = new NetworkCredential(u, p);
return networkCreds;
}
}
我需要通过代理中继 C# Graph-Sdk 发出的 HTTP 请求。
在文档中我找不到任何关于代理设置的信息。 我目前找到的唯一解决方法是更改全局代理设置:
System.Net.GlobalProxySelection.Select = proxy;
or
System.Net.WebRequest.DefaultWebProxy = proxy;
遗憾的是,在我的情况下,如果不移动所有图表,这是不可能的 相关功能到一个单独的进程中(因为主进程的其余部分需要 运行 没有代理)。
所以我的问题是:
sdk有官方支持代理设置吗?
未来的 sdk 版本是否计划支持代理设置?
您可以在实例化 GraphServiceClient 时设置代理。
2021 年 6 月 9 日更新
现在有一个更好的方法,就是使用 GraphClientFactory。
HttpClient httpClient = GraphClientFactory.Create(GetClientCredentialProvider(), "v1.0", "Global", new WebProxy(""));
var graphServiceClient = new(httpClient);
旧答案
System.Net.Http.HttpClientHandler httpClientHandler = new System.Net.Http.HttpClientHandler()
{
AllowAutoRedirect = false,
Proxy = new WebProxy() // TODO: Set your proxy settings.
};
HttpProvider httpProvider = new HttpProvider(httpClientHandler, true);
GraphServiceClient client = new GraphServiceClient("https://graph.microsoft.com/v1.0",
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
var token = await goGetSomeTokenNow();
requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", token);
}), httpProvider);
this 文章中有 Microsoft.Graph 4+ 的新方法。然而,由于我们的 azure adfs 只有 auth 请求来自浏览器,因此我必须混合使用两种方式来实现这一点。希望这对其他人有帮助。
private GraphServiceClient GetGraphClient()
{
string[] scopes = new string[] { "User.Read", "User.ReadBasic.All", "Mail.Read", "Mail.ReadWrite", "Mail.Send" };
var msalFactory = new MsalHttpClientFactoryOwn(_configuration);
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create("")
.WithTenantId("")
.WithHttpClientFactory(msalFactory)
.Build();
UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication, scopes);
HttpClient httpClient = GraphClientFactory.Create(authProvider, "v1.0", "Global", msalFactory.GetWebProxy());
var graphClient = new GraphServiceClient(httpClient);
return graphClient;
}
public class MsalHttpClientFactoryOwn : IMsalHttpClientFactory
{
private readonly IConfiguration configuration;
public ProxiedHttpClientFactory(IConfiguration configuration)
{
this.configuration = configuration;
}
public HttpClient GetHttpClient()
{
var proxyHttpClientHandler = new HttpClientHandler()
{
UseProxy = true,
UseDefaultCredentials = false,
Credentials = GetNetworkCredentials(),
Proxy = GetWebProxy()
};
var httpClient = new HttpClient(proxyHttpClientHandler);
httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
httpClient.Timeout = TimeSpan.FromMinutes(30);
return httpClient;
}
public WebProxy GetWebProxy()
{
var proxy = new WebProxy
{
Address = new Uri("proxy address"),
BypassProxyOnLocal = false,
UseDefaultCredentials = false,
Credentials = GetNetworkCredentials()
};
return proxy;
}
private NetworkCredential GetNetworkCredentials()
{
var networkCreds = new NetworkCredential(u, p);
return networkCreds;
}
}