-
Notifications
You must be signed in to change notification settings - Fork 343
System Browser on .Net Core
Note: this feature is available from version 4.0.0 on .Net Core and from version 4.1.0 on .Net Classic and .Net Standard. B2C does not currently support this mode of authentication.
MSAL is a multi-framework library and has framework specific code to host a browser in a UI control (e.g. on .Net Classic it uses WinForms, on Xamarin it uses native mobile controls etc.). This is called embedded
web ui.
Alternatively, MSAL is also able to kick off the system OS browser.
Generally, we recommend that you use the platform default, and this is typically the system browser. The system browser is better at remembering the users that have logged in before. If you need to change this behavior, use WithUseEmbeddedWebView(bool)
On .NET Core, MSAL will start the system browser as a separate process. MSAL does not have control over this browser, but once the user finishes authentication, the web page is redirected in such a way that MSAL can intercept the Uri.
You can also configure apps written for .NET Classic to use this browser, by specifying
await pca.AcquireTokenInteractive(s_scopes)
.WithUseEmbeddedWebView(false)
MSAL cannot detect if the user navigates away or simply closes the browser. Apps using this technique are encouraged to define a timeout (via CancellationToken
). We recommend a timeout of at least a few minutes, to take into account cases where the user is prompted to change password or perform 2FA.
MSAL needs to listen on on http://localhost:port
and intercept the code that AAD sends when the user is done authenticating.
To achieve this:
- During app registration, configure
http://localhost
as a redirect uri (not currently supported by B2C) - When you construct your PubliClientApplication, specify this redirect uri:
IPublicClientApplication pca = PublicClientApplicationBuilder
.Create("<CLIENT_ID>")
// or use a known port if you wish "http://localhost:1234"
.WithRedirectUri("http://localhost")
.Build();
Note: If you configure http://localhost
, internally MSAL will find a random open port and use it.
On Linux, MSAL will open the default OS browser using the xdg-open tool. To troubleshoot, run the tool from a terminal e.g. xdg-open "https://www.bing.com"
On Mac, the browser is opened by invoking open <url>
Note: customization is available from MSAL 4.1.0 onward
MSAL is able to respond with an HTTP message when a token is received or in case of error. You can display an HTML message or redirect to an url of your choice:
var options = new SystemWebViewOptions()
{
HtmlMessageError = "<p> An error occured: {0}. Details {1}</p>",
BrowserRedirectSuccess = new Uri("https://www.microsoft.com");
}
await pca.AcquireTokenInteractive(s_scopes)
.WithUseEmbeddedWebView(false)
.WithSystemWebViewOptions(options)
.ExecuteAsync();
You may customize the way MSAL opens the browser. For example instead of using whatever browser is the default, you can force open a specific browser:
var options = new SystemWebViewOptions()
{
OpenBrowserAsync = SystemWebViewOptions.OpenWithEdgeBrowserAsync
}
- Home
- Why use MSAL.NET
- Is MSAL.NET right for me
- Scenarios
- Register your app with AAD
- Client applications
- Acquiring tokens
- MSAL samples
- Known Issues
- AcquireTokenInteractive
- WAM - the Windows broker
- .NET Core
- Maui Docs
- Custom Browser
- Applying an AAD B2C policy
- Integrated Windows Authentication for domain or AAD joined machines
- Username / Password
- Device Code Flow for devices without a Web browser
- ADFS support
- Acquiring a token for the app
- Acquiring a token on behalf of a user in Web APIs
- Acquiring a token by authorization code in Web Apps
- High Availability
- Token cache serialization
- Logging
- Exceptions in MSAL
- Provide your own Httpclient and proxy
- Extensibility Points
- Clearing the cache
- Client Credentials Multi-Tenant guidance
- Performance perspectives
- Differences between ADAL.NET and MSAL.NET Apps
- PowerShell support
- Testing apps that use MSAL
- Experimental Features
- Proof of Possession (PoP) tokens
- Using in Azure functions
- Extract info from WWW-Authenticate headers
- SPA Authorization Code