This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
with proxy
Bogdan Gavril edited this page Jan 22, 2019
·
14 revisions
ADAL communicates with AAD using HttpClient. There are many scenarios where you need to customise this HttpClient, the most common scenario being having to use a proxy server.
Starting with ADAL 4.5.0, a new constructor for AuthenticationContext that takes in an HttpClient has been added.
HttpClient httpClient = CreateCustomHttpClient(); // set a proxy, add headers etc.
AuthenticationContext authenticationContext = new AuthenticationContext(
authority: "https://login.microsoftonline.com/common",
validateAuthority: true,
// on .Net and .Net core define your own cache persistence (omitted here for brevity)
tokenCache: TokenCache.DefaultShared,
httpClient: httpClient);
// Now call into ADAL using the standard pattern - AcquireTokenSilent then AcquireToken
This is how to specify a proxy. For more details see the MSDN docs.
var proxy = new WebProxy()
{
Address = new Uri("localhost:8080"),
BypassOnLocal = false,
UseDefaultCredentials = false,
// *** These creds are given to the proxy server, not the web server ***
Credentials = new NetworkCredential(
userName: "proxyUserName",
password: "proxyPassword");
};
// Now create a client handler which uses that proxy
var httpClientHandler = new HttpClientHandler()
{
Proxy = proxy,
};
// Finally, create the HTTP client object
var client = new HttpClient(handler: httpClientHandler, disposeHandler: true);
For reference, this is how ADAL configures its HttpClient if you don't specify one:
var httpClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true })
{
MaxResponseContentBufferSize = 1 * 1024 * 1024 // 1 MB
};
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Previous to ADAL 4.5.0 ADAL.NET does not propose any API to set a proxy, however you can use the system level API: the HttpClients used by ADAL.NET are able to use it.
using System.Net;
...
{
// Set the proxy
IWebProxy proxy = new WebProxy("http://proxyserver:80", true);
// This does not work on .Net Core https://github.com/dotnet/corefx/issues/7037
WebRequest.DefaultWebProxy = proxy;
// Use ADAL.NET
}
- Home
- Why use ADAL.NET?
- Register your app with AAD
- AuthenticationContext
- Acquiring Tokens
- Calling a protected API
- Acquiring a token interactively
- Acquiring tokens silently
- Using Device Code Flow
- Using Embedded Webview and System Browser in ADAL.NET and MSAL.NET
- With no user
- In the name of a user
- on behalf of (Service to service calls)
- by authorization code (Web Apps)
- Use async controller actions
- Exception types
- using Broker on iOS and Android
- Logging
- Token Cache serialization
- User management
- Using ADAL with a proxy
- Authentication context in multi-tenant scenarios
- Troubleshooting MFA in a WebApp or Web API
- Provide your own HttpClient
- iOS Keychain Access