Proxy Configuration for C# GraphSdk
Asked Answered
H

2

6

I need to relay the HTTP-Requests made by the C# Graph-Sdk over a proxy.

In the documentation I could not find any information about proxy settings. The only workaraound I currently found is to change the global proxy settings:

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

Sadly in my situation this is not possible without moving all graph related features into a separate process (as the rest of the main-process needs to run without proxy).

So my Question is:

  • Is there any official support for Proxy Settings in the sdk?

  • And is support for Proxy Settings planned for future sdk-versions?

Humphrey answered 13/2, 2018 at 14:40 Comment(0)
D
7

You can set the proxy when you instantiate your GraphServiceClient.

Update 6/9/2021

There is now a better way by using the GraphClientFactory.

HttpClient httpClient = GraphClientFactory.Create(GetClientCredentialProvider(), "v1.0", "Global", new WebProxy(""));
var graphServiceClient = new(httpClient);

Old answer

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);
Dermatologist answered 13/2, 2018 at 22:19 Comment(2)
Hi Is there a Java equivalent?Bilge
@sh4r4d Do you mean to ask whether there is a Java equivalent library or way to set the proxy? Yes to both.Dermatologist
U
3

There is a new way for Microsoft.Graph 4+ in this article. Yet I had to achieve this using mix of two ways due to our azure adfs only auth requests are coming from browsers. Hope this will help full to others.

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;
    }
}
Underlayer answered 27/7, 2021 at 4:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.