question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid State errror on using with OidcClient

See original GitHub issue

I tried using the WebAuthenticator class with OidcClient library as a replacement for Microsoft.Maui.Authentication.WebAuthenticator in a Maui desktop application. Unlike the latter, the web browser opens now and on receiving the credentials, redirects back to the application. But the when the returned WebAuthenticatorResult object causes Invalid State error in OidcClient library.

If I instead, use the WinUIEx.WebAuthenticatorResult class too, I get Malformed Callback Url error.

The code works perfectly when used on Android with Maui’s WebAuthenticator. On closer inspection, the value for the “state” key in the returned WebAuthenticatorResult.Properties dictionary (from AuthenticateAsync() method) seems quite different from what is received on a similar android project.

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;

#if WINDOWS
               // THIS IS WHERE THE DIFFERENCE IS WITH ANDROID. Here, authResult.Properties dictionary contains
               // a key "state" whose value seems very different from that in case of android
                var authResult =
                    await WinUIEx.WebAuthenticator.AuthenticateAsync(new Uri(options.StartUrl), new Uri(callbackUrl));
#else
                // Executed in android
                WebAuthenticatorResult authResult =
                    await WebAuthenticator.AuthenticateAsync(new Uri(options.StartUrl), new Uri(callbackUrl));
#endif
                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()
                };
            }
        }

        public string ToRawIdentityUrl(string redirectUrl, WebAuthenticatorResult result)
        {
            IEnumerable<string> parameters = result.Properties.Select(pair => $"{pair.Key}={pair.Value}");
            var values = string.Join("&", parameters);

            return $"{redirectUrl}#{values}";
        }

#if WINDOWS
        public string ToRawIdentityUrl(string redirectUrl, WinUIEx.WebAuthenticatorResult result)
        {
            IEnumerable<string> parameters = result.Properties.Select(pair => $"{pair.Key}={pair.Value}");
            var values = string.Join("&", parameters);

            return $"{redirectUrl}#{values}";
        }
#endif
    }

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
shv07commented, May 5, 2022

v1.4.2 works perfectly as expected. Appreciate the help.

1reaction
dotMortencommented, May 5, 2022

v1.4.2 should be available on nuget in 10-15 mins. Let me know if that fixes it for you

Read more comments on GitHub >

github_iconTop Results From Across the Web

AppAuth oidc invalid State error when authenticating ...
I've configured an identity server 3 as an IdP for a project, we have 3 clients: MVC web, IOS and Android. everything is...
Read more >
When authenticating with OpenID, a user is not able to log ...
OidcAuthenticator: OpenID Connect authentication failed com.spotfire.server.security.auth.oidc.OidcException: Invalid state parameter ...
Read more >
Invalid state error when using /authorize endpoint directly ...
Hi,. I'm making a request to the /authorize endpoint which is redirecting me to the login page where I enter my credentials and...
Read more >
Troubleshoot WordPress Plugin Invalid State Errors
The most common cause of the invalid state error is when the callback URL is cached on the server. Exclude caching on your...
Read more >
OpendIDConnect issues, invalid 'state' parameter and ...
I have checked and state parameter in the call to my oauth provider matches the state parameter in the URL fragment in both...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found