Skip to content

[Auth][Desktop/Skia] Add support for WebAuthenticationBroker for OIDC/Web #2640

Description

@kazo0

Use something similar to https://github.com/IdentityModel/IdentityModel.OidcClient.Samples/blob/main/NetCoreConsoleClient/src/NetCoreConsoleClient/SystemBrowser.cs for a browser implementation that can support net-desktop targets

I believe @carldebilly has a modified version of this that can do away with the Kestrel dependency.

Activity

  1. kazo0 commented on Dec 9, 2024

    @kazo0
    ContributorAuthor

    Poking @carldebilly / @nickrandolph for anymore info that we need for this one.

    Doing this will allow us to no longer rely on the WebAuthenticationBroker, correct?

  2. carldebilly commented on Dec 9, 2024

    @carldebilly
    Member

    Why would you want to remove the dependency on Web Auth Broker? It's a robust mechanism and less proprietary than the IBrowser interface. In the latest WinUI, Microsoft introduced a new OAuth2Manager in the latest version of WinAppSdk (1.7), so we should consider aligning with it.

    Actually, I did something slightly different: instead of customizing the IBrowser interface specific to EntityModel.OidcClient (now renamed Duende.IdentityModel.OidcClient), I implemented a custom WebAuthenticationBroker within Uno (leveraging Uno's built-in API extensibility mechanisms) and used the AutoRedirectUri feature to correctly resolve the URI from the browser.

    The missing piece is the custom Web Authentication Broker (WAB), which uses my "new" http server. This server is extremely lightweight, has no dependencies, and is far easier to use than Kestrel. Check it out here: https://www.nuget.org/packages/Yllibed.HttpServer/1.0.0-dev.10.ge2ac37743f#readme-body-tab

    Code:

    #if HAS_UNO_SKIA
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using Windows.Security.Authentication.Web;
    using Uno.AuthenticationBroker;
    using Uno.Foundation.Extensibility;
    using Yllibed.HttpServer;
    using Yllibed.HttpServer.Handlers;
    
    [assembly:
    	ApiExtension(typeof(IWebAuthenticationBrokerProvider), typeof(SystemBrowserAuthBroker))]
    
    namespace MyApp.Services.Auth;
    
    public sealed class SystemBrowserAuthBroker : IWebAuthenticationBrokerProvider
    {
    	private Server? _server;
    
    	private Uri? _serverRootUri;
    
    	public Uri GetCurrentApplicationCallbackUri()
    	{
    		return new Uri(EnsureServerStarted().RootUri, "/auth-callback");
    	}
    
    	public async Task<WebAuthenticationResult> AuthenticateAsync(WebAuthenticationOptions options, Uri requestUri,
    		Uri callbackUri, CancellationToken ct)
    	{
    		if (options.HasFlag(WebAuthenticationOptions.SilentMode))
    		{
    			throw new NotSupportedException("SilentMode is not supported by this broker.");
    		}
    
    		if (options.HasFlag(WebAuthenticationOptions.UseTitle))
    		{
    			throw new NotSupportedException("UseTitle is not supported by this broker.");
    		}
    
    		if (options.HasFlag(WebAuthenticationOptions.UseHttpPost))
    		{
    			throw new NotSupportedException("UseHttpPost is not supported by this broker.");
    		}
    
    		if (options.HasFlag(WebAuthenticationOptions.UseCorporateNetwork))
    		{
    			throw new NotSupportedException("UseCorporateNetwork is not supported by this broker.");
    		}
    
    		var (server, _) = EnsureServerStarted();
    		var authCallbackHandler = new AuthCallbackHandler(callbackUri);
    		using (server.RegisterHandler(authCallbackHandler))
    		{
    			OpenBrowser(requestUri);
    			return await authCallbackHandler.WaitForCallbackAsync();
    		}
    	}
    
    	private (Server Server, Uri RootUri) EnsureServerStarted()
    	{
    		if (_server is null || _serverRootUri is null)
    		{
    			_server = new Server();
    			(_serverRootUri, _) = _server.Start();
    		}
    
    		return (_server, _serverRootUri);
    	}
    
    	public static void OpenBrowser(Uri uri)
    	{
    		var url = uri.AbsoluteUri;
    		try
    		{
    			Process.Start(url);
    		}
    		catch
    		{
    			// hack because of this: https://github.com/dotnet/corefx/issues/10361
    			if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    			{
    				url = url.Replace("&", "^&");
    				Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
    			}
    			else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
    			{
    				Process.Start("xdg-open", url);
    			}
    			else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ||
    			         RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
    			{
    				Process.Start("open", url);
    			}
    			else
    			{
    				throw;
    			}
    		}
    	}
    
    
    	private sealed class AuthCallbackHandler(Uri callbackUri) : IHttpHandler
    	{
    		private readonly TaskCompletionSource<WebAuthenticationResult> _tcs = new();
    
    		public Task HandleRequest(CancellationToken ct, IHttpServerRequest request, string relativePath)
    		{
    			if (request.Url.AbsolutePath.StartsWith(callbackUri.AbsolutePath, StringComparison.OrdinalIgnoreCase))
    			{
    				var result = new WebAuthenticationResult(
    					request.Url.OriginalString,
    					200,
    					WebAuthenticationStatus.Success);
    
    				_tcs.TrySetResult(result);
    				request.SetResponse("text/plain", "Auth completed - you can close this browser now.");
    			}
    
    			return Task.CompletedTask;
    		}
    
    		public Task<WebAuthenticationResult> WaitForCallbackAsync()
    		{
    			return _tcs.Task;
    		}
    	}
    }
    #endif
  3. changed the title [-][Auth] Use custom IBrowser for OIDC/MSAL[/-] [+][Auth][Desktop/Skia] Add support for WenAuthenticationBroker for OIDC/MSAL[/+] on Jul 21, 2025
  4. changed the title [-][Auth][Desktop/Skia] Add support for WenAuthenticationBroker for OIDC/MSAL[/-] [+][Auth][Desktop/Skia] Add support for WebAuthenticationBroker for OIDC/MSAL[/+] on Jul 21, 2025
  5. DevTKSS commented on Aug 2, 2025

    @DevTKSS
    Contributor

    @carldebilly do you see a chance that this could get https available and that we could pass in the relative path dynamically?
    that would allow us to have multiple providers registred. Like if we would have Amazon and like I am currently Etsy, it would be best to provide that like for the http handler in appsettings as "Url" = "/signin-amazon" or "Url" = "/callback-etsy" 🤔
    or does the "AuthCallbackHandler" in some other way get available (currently private?) to allow us configuring that CallbackUri?

  6. kazo0 commented on Aug 11, 2025

    @kazo0
    ContributorAuthor

    @carldebilly should your SystemBrowserAuthBroker be defined and registered via APIExtension inside of the Uno Extensions Auth library itself?

    Would this be a temporary way of handling auth until unoplatform/uno#19787 is implemented?

    I'm not sure where/how to use this IWebAuthenticationBrokerProvider implementation when using Uno Auth MSAL/OIDC Extensions

  7. self-assigned this
    on Aug 15, 2025
  8. added theissue type on Oct 29, 2025
  9. changed the issue type fromtoon Oct 29, 2025
  10. changed the issue type fromtoon Nov 14, 2025
  11. changed the issue type fromtoon Nov 14, 2025
  12. DevTKSS commented on Jan 12, 2026

    @DevTKSS
    Contributor

    @kazo0

    1. OidcAuthenticationProvider likely still fails on Desktop/Skia

      Even though the IdentityModel.OidcClient namespace now matches the rebranded Duende.IdentityModel.OidcClient in the latest Uno.Extensions, I would still expect Desktop/Skia to fail because OidcAuthenticationProvider relies on: WebAuthenticationBroker.GetCurrentApplicationCallbackUri()

      public void Build()
      {
      var config = Settings?.Options ?? Configuration.Get(Name) ?? new OidcClientOptions();
      if (Settings is { AutoRedirectUri: true })
      {
      config.RedirectUri = config.PostLogoutRedirectUri = WebAuthenticationBroker
      .GetCurrentApplicationCallbackUri().OriginalString;
      }
      config.Browser = Browser;
      _client = new OidcClient(config);
      }

      …but WebAuthenticationBroker isn’t implemented on Desktop, and we also don’t have the usual csproj/Win32-manifest style “registered callback URI” support you’d know from classic WinForms/WPF frameworks. 🫤

    2. WebAuthenticationProvider hard-depends on WebAuthenticationBroker.AuthenticateAsync Uno.Extensions.Authentication.UI’s WebAuthenticationProvider is strongly coupled to: WebAuthenticationBroker.AuthenticateAsync(...)

      #if WINDOWS
      var userResult = await WinUIEx.WebAuthenticator.AuthenticateAsync(new Uri(loginStartUri), new Uri(loginCallbackUri));
      var authData = string.Join("&", userResult.Properties.Select(x => $"{x.Key}={x.Value}"))??string.Empty;
      #else
      var userResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, new Uri(loginStartUri), new Uri(loginCallbackUri));
      var authData = userResult?.ResponseData ?? string.Empty;
      #endif

    3. IWebAuthenticationBrokerProvider can’t be DI-resolved due to ApiExtensibility.CreateInstance

      I tried registering my own IWebAuthenticationBrokerProvider via IServiceCollection, but it can’t be resolved

    4. WebAuthenticationBroker ignores IServiceCollection registrations because WebAuthenticationBroker uses ApiExtensibility.CreateInstance(null, out _authenticationBrokerProvider);. So even if an implementation of IWebAuthenticationBrokerProvider exists in DI, it won’t be used

    5. ApiExtensibilityAttribute doesn’t support ctor args (and null likely blocks it anyway)

      The [ApiExtensibility(...)] mechanism doesn’t allow passing ctor arguments, and the explicit null passed into CreateInstance(...) appears to prevent any pathway for DI-based creation.

      https://github.com/unoplatform/uno/blob/f5cc992e78fcac760f4a9dbde445306eea5aaff7/src/Uno.Foundation/Extensibility/ApiExtensibility.cs#L71-L96

    6. While it would be really cool and straight forward, the Walkthrought for Web Auth in the Docs is false stating that it will be using WebView which is not the case! [WebView] [Auth] Wrong Docs title suggesting to be able to use Web Auth provider with WebView #2972


    Why I’m bringing this up (practical integration angle)

    I tried the above with the AuthCallbackHandler from my PR against carldebilly/Yllibed.HttpServer:

    This handler could act (at least in Development) as the local OAuth callback backend for Uno apps eventually even for the currently not WebView using Web Auth as stated in the Docs

    It’s possible to integrate cleanly, with (or without) support of IOptions<T>, comes with tests, and includes a dedicated integration path for WebAuthenticationResult - which WebAuthenticationProvider currently expects from WebAuthenticationBroker.

    But due to (4) + (5), there’s currently no workable way to integrate such a provider into the existing Uno.Extensions.Authentication flow on Desktop/Skia.

    @Jen-Uno FYI - Maybe you could take a look if there is some progress available for this? Merging the PR to allow a DI version of the IWebAuthenticationBrokerProvider in the WebAuthenticationProvider would from my POV be the lowest effort option to have a working Desktop Auth Workaround until someone comes up with a other / better solution 👍

  13. self-assigned this
    on Apr 28, 2026
  14. removed
    kind/enhancement
    on Aug 7, 2026
  15. kazo0 commented on Aug 12, 2026

    @kazo0
    ContributorAuthor

    Re-scoping this issue to OIDC/Web — dropping MSAL from its scope.

    MSAL no longer needs a WebAuthenticationBroker on Desktop/Skia:

    • MsalAuthenticationProvider never uses the WAB on desktop targets — MSAL.NET drives interactive sign-in itself via the system browser + loopback redirect (http://localhost). The provider's only WAB call (WithWebRedirectUri()) is gated to WebAssembly, where the broker has a working implementation.
    • With fix(msal): Deploy platform MSAL assets under SkiaRenderer uno#24055 (the fix for [Auth] MSAL Platform specific code does not work in SkiaRenderer mode uno#20601, wrong Uno.UI.MSAL flavor deployed under UnoFeatures=SkiaRenderer), interactive MSAL sign-in was validated live on Skia-Desktop (system browser + loopback), Skia-Android (custom tab), and Skia-WASM (popup via WasmWebUi).

    What remains — and why this issue stays open:

    • OidcAuthenticationProvider drives its browser through WebAuthenticatorBrowser, which wraps WebAuthenticationBroker.AuthenticateAsync.
    • The generic WebAuthenticationProvider (AddWeb) calls WebAuthenticationBroker.AuthenticateAsync directly.
    • Uno's default Skia broker (WebAuthenticationBrokerProvider.skia.cs) throws NotImplementedException, so both providers are non-functional on Desktop/Skia unless the app registers a custom IWebAuthenticationBrokerProvider (like @carldebilly's SystemBrowserAuthBroker above).

    Open design question for whoever picks this up: a default in-Uno WAB implementation (e.g. the lightweight-HTTP-server approach above) vs. an OidcClient IBrowser implementation in this repo vs. aligning with WinAppSdk 1.7's OAuth2Manager.

    Separate note: MSAL's WAM broker (WithBroker) is currently only wired on the WinAppSdk head; enabling WAM SSO for desktop-TFM apps running on Windows would be an MSAL enhancement independent of the WebAuthenticationBroker.

  16. changed the title [-][Auth][Desktop/Skia] Add support for WebAuthenticationBroker for OIDC/MSAL[/-] [+][Auth][Desktop/Skia] Add support for WebAuthenticationBroker for OIDC/Web[/+] on Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions