C#GraphSdk的代理配置

时间:2018-02-13 14:41:00

标签: c# microsoft-graph http-proxy

我需要通过代理中继C#Graph-Sdk发出的HTTP请求。

在文档中,我找不到任何有关代理设置的信息。 我目前发现的唯一的工作方式是更改全局代理设置:

System.Net.GlobalProxySelection.Select = proxy;
or
System.Net.WebRequest.DefaultWebProxy = proxy;

可悲的是,在我的情况下,如果不移动所有图形,这是不可能的 相关功能进入一个单独的进程(因为主进程的其余部分需要在没有代理的情况下运行)。

所以我的问题是:

  • sdk中是否有对代理设置的官方支持?

  • 是否支持为未来的sdk版本计划的代理设置?

2 个答案:

答案 0 :(得分:3)

您可以在实例化GraphServiceClient时设置代理。

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);

答案 1 :(得分:2)

this 文章中为 Microsoft.Graph 4+ 提供了一种新方法。然而,由于我们的 azure adfs 仅来自浏览器的身份验证请求,我不得不混合使用两种方式来实现这一点。希望这对其他人有帮助。

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;
    }
}