Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proxy Configuration for C# GraphSdk

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?

like image 352
Karlheinz Reinhardt Avatar asked Feb 13 '18 14:02

Karlheinz Reinhardt


2 Answers

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);
like image 86
Michael Mainer Avatar answered Oct 19 '22 10:10

Michael Mainer


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;
    }
}
like image 32
cdev Avatar answered Oct 19 '22 08:10

cdev