dotMorten / WinUIEx

WinUI Extensions
https://dotmorten.github.io/WinUIEx
MIT License
602 stars 38 forks source link

Browser tab does not close after authentication in some cases #43

Closed shv07 closed 2 years ago

shv07 commented 2 years ago

I am using WinUiEx.WebAuthenticator with OidcClient library for authentication in my MAUI app. Here, the web browser does not close after a redirect to the app (on clicking "Open in MAUI app") if the user was not already logged in on that browser.

On the other hand, if I log in on the browser manually beforehand and then try logging in using the desktop app, then the browser opens but you don't have to re-enter your credentials and closes after you click "Open in MAUI app" button in the browser.

This issue occurs in both Chrome and Edge.

The relevant piece of code which calls the method of this class is here:-

internal class WebAuthenticatorBrowser : IdentityModel.OidcClient.Browser.IBrowser
    {
        private readonly string _callbackUrl;

        public WebAuthenticatorBrowser(string callbackUrl = null)
        {
            _callbackUrl = callbackUrl ?? null;
        }
        public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = default)
        {
            try
            {
                var callbackUrl = string.IsNullOrEmpty(_callbackUrl) ? options.EndUrl : _callbackUrl;

                var authResult =
                    await WinUIEx.WebAuthenticator.AuthenticateAsync(new Uri(options.StartUrl), new Uri(callbackUrl));
                var authorizeResponse = ToRawIdentityUrl(options.EndUrl, authResult);

                return new BrowserResult
                {
                    Response = authorizeResponse
                };
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return new BrowserResult()
                {
                    ResultType = BrowserResultType.UnknownError,
                    Error = ex.ToString()
                };
            }
        }
dotMorten commented 2 years ago

This is just how oauth works when using the users preferred browser. All apps using this workflow have this happen