doc/articles/features/web-authentication-broker.md
WinRTFeatureConfiguration.WebAuthenticationBroker.AuthenticationTimeout.<iframe> mode (see advanced usages below), the server must allow for using CSP (Content Security Policy).<origin>/authentication-callback. For example http://localhost:5000/authentication-callback.WinRTFeatureConfiguration.WebAuthenticationBroker .Info.plist of the application.<scheme>:/authentication-callback. Ex: my-app-auth:/authentication-callbackWinRTFeatureConfiguration.WebAuthenticationBroker.DefaultReturnUri property.The redirect URI MUST use a custom scheme URI. This one will launch a special Activity declared in the application.
You MUST declare an activity inheriting from WebAuthenticationBrokerActivityBase in the Android head. Note the [Activity] attribute needs to include Exported = true if you are targeting Android 12.
// Android: add this class near the MainActivity, in the head project
[Activity(NoHistory = true, LaunchMode = LaunchMode.SingleTop, Exported = true)]
[IntentFilter(
new[] {Android.Content.Intent.ActionView},
Categories = new[] {Android.Content.Intent.CategoryDefault, Android.Content.Intent.CategoryBrowsable},
// To be changed for a scheme specific to the application
DataScheme = "myapplication")]
public class WebAuthenticationBrokerActivity : WebAuthenticationBrokerActivityBase
{
// Note: the name of this class is not important
}
To use the automatic discovery of the redirect URI, it is required to set the IntentFilter using the attributes like in the previous point. If you put it in the manifest, you'll need to set the URI using the WinRTFeatureConfiguration.WebAuthenticationBroker.DefaultReturnUri property.
Default redirect URI will be <scheme>:/authentication-callback. Ex: my-app-auth:/authentication-callback
The default implementation of the WebAuthenticationBroker on Android will launch the system browser and the result will come back through the custom scheme of the Return Uri. The AndroidX Chrome Custom Tabs may also be used. Advanced section below contains instructions about this.
For special needs, it is possible to create a custom implementation of the Web Authentication Broker by using the [ApiExtension] mechanism of Uno and implementing the IWebAuthenticationBrokerProvider interface:
[assembly: ApiExtension(typeof(MyNameSpace.MyBrokerImplementation), typeof(Uno.AuthenticationBroker.IWebAuthenticationBrokerProvider))]
public class MyBrokerImplementation : Uno.AuthenticationBroker.IWebAuthenticationBrokerProvider
{
Uri GetCurrentApplicationCallbackUri() => [TODO]
Task<WebAuthenticationResult> AuthenticateAsync(WebAuthenticationOptions options, Uri requestUri, Uri callbackUri, CancellationToken ct)
{
[TODO]
}
}
This implementation can also published as a NuGet package and it will be discovered automatically by the Uno tooling during compilation.
<iframe> instead of a browser windowOn WebAssembly, it is possible to use an in-application <iframe> instead of opening a new window. Beware the authentication server must support this mode.
Create an <iframe> control:
[HtmlElement("iframe")]
public class LoginIFrame : Control
{
public LoginIFrame()
{
// A background is required to allow interactions
// with the control
Background = new SolidColorBrush(Colors.Transparent);
}
}
Use the LoginIFrame control in the page:
<Page ...>
<Grid>
[...]
<controls:LoginIFrame x:name="loginWebView" />
</Grid>
</Page>
Set the HtmlId before calling the WebAuthenticationBroker:
private async void LoginHidden_Click(object sender, RoutedEventArgs e)
{
// Set configuration to use the control as the iframe control
WinRTFeatureConfiguration.WebAuthenticationBroker.IFrameHtmlId = loginWebView.GetHtmlId();
try
{
var userResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, _startUri);
[...]
}
finally
{
// Don't forget to reset it when finished
WinRTFeatureConfiguration.WebAuthenticationBroker.IFrameHtmlId = null;
}
}
NOTES:
<iframe>, you don't need to create a control, you can simply use the WebAuthenticationOptions.SilentMode as the first parameter to WebAuthenticationBroker.AuthenticateAsync().