I am having problems getting the dotnet graph sdk to use my proxy settings as expected. I have tried setting the proxy when initializing the HttpClient with the GraphClientFactory but still seem to get the exception I am showing below. These same settings are being passed to the ConfidentialClientApplicationBuilder and is being used to succsesfully request the authentication token. It is also communicating to internal application API's through the same proxy settings with no problems. Can someone please help point me in the right direction? I am using the most recent version of the Graph SDK 4.36.0.0
public async Task Initialize(WebProxy webProxy)
{
var httpClientFactory = new GraphIntegrationHttpClientFactory(webProxy);
IConfidentialClientApplication daemonClient;
//Creates a daemon service.
daemonClient = ConfidentialClientApplicationBuilder.Create(_clientId)
.WithAuthority(string.Format(Constants.AuthorityFormat, _tenantId))
.WithClientSecret(_clientSecret)
.WithHttpClientFactory(httpClientFactory)
.Build();
//Gets auth token.
AuthenticationResult authResult = await daemonClient.AcquireTokenForClient(new string[] { Constants.MsGraphScope })
.ExecuteAsync();
ClientCredentialProvider authProvider = new ClientCredentialProvider(daemonClient);
var handlers = GraphClientFactory.CreateDefaultHandlers(authProvider);
handlers.Add(new LoggingHandler(_logService));
var httpClient = GraphClientFactory.Create(handlers, proxy: webProxy);
httpClient.Timeout = TimeSpan.FromMinutes(5);
_graphServiceClient = new GraphServiceClient(httpClient);
}
Exception
Microsoft.Graph.ServiceException: Code: generalException
Message: An error occurred sending the request.
---> System.InvalidOperationException: When using a non-null Proxy, the WindowsProxyUsePolicy property must be set to WindowsProxyUsePolicy.UseCustomProxy.
at Application.Common.Graph.Handlers.LoggingHandler.<SendAsync>d__2.MoveNext() in C:\Users\DamienOstler\Documents\GitHub\Application\src\main\Application.Common.Graph\Handlers\LoggingHandler.cs:line 42
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.RedirectHandler.<SendAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.RetryHandler.<SendAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.CompressionHandler.<SendAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.AuthenticationHandler.<SendAsync>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.SimpleHttpProvider.<SendRequestAsync>d__13.MoveNext()
--- End of inner exception stack trace ---
at Microsoft.Graph.SimpleHttpProvider.<SendRequestAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.SimpleHttpProvider.<SendAsync>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.BaseRequest.<SendRequestAsync>d__40.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.BaseRequest.<SendAsync>d__34`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.UserRequest.<GetAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Application.Common.Graph.OneDriveIntegration.<GetUsers>d__10.MoveNext() in C:\Users\DamienOstler\Documents\GitHub\Application\src\main\Application.Common.Graph\OneDriveIntegration.cs:line 144
I have found a way to get around this problem, by passing a customer HttpClientHandler with all the information set as the finalhandler parameter for the GraphClientFactory Create method. I have opened a new issue on the github page about passing the proxy not working properly.