auth0-samples / auth0-xamarin-oidc-samples

Auth0 OIDC Client with Xamarin applications
MIT License
30 stars 38 forks source link

Auth0 authentication user cancel browser event not triggered for Xamarin Forms Android #38

Closed basilkurianmb closed 1 year ago

basilkurianmb commented 4 years ago

For Xamarin Forms Android, the await _auth0Client.LoginAsync()is not returning any cancelled event while the browser is cancelled manually by user. For iOS, at-least it is returning null. Is there is any solution for it? Thanks for considering this topic.

frederikprijck commented 4 years ago

As our Auth0Client is dependent on the OidcClient.LoginAsync method (https://github.com/auth0/auth0-oidc-client-net/blob/master/src/Auth0.OidcClient.Core/Auth0ClientBase.cs#L59), this sounds like an issue with the OidcClient that is being used.

There is a related issue https://github.com/IdentityModel/IdentityModel.OidcClient/issues/54. Even tho it is closed, it doesn't look like it is fixed. Might want to discuss the issue to their repository.

JonnySKK commented 3 years ago

Encountered the same issue. I found that when it authenticates successfully, it correctly triggers OnNewIntent within the activity and therefore hits the line ActivityMediator.Instance.Send(intent.DataString); but when its cancelled, i.e. the browser is closed, then it just hangs on loginasync, never triggering OnNewIntent.

However, on closing the browser, I found it does still trigger OnResume. So my workaround was to add a boolean to the activity called "IsAuthenticating" which is set to true just before LoginAsync is triggered, and then add this to OnResume:

        protected override void OnResume()
        {
            base.OnResume();

            if (IsAuthenticating)
            {
                ActivityMediator.Instance.Send("com.company.myapp://myauthdomain.eu.auth0.com/android/com.company.myapp/callback");
            }
        }

This seems to finish the await loginasync and allow the app to continue. I also set the IsAuthenticating to false in OnNewIntent if the login is successful, so that when it hits onresume during a successful login, the ActivityMediator send isn't triggered.

FredBesterwitch commented 3 years ago

@JonnySKK Did you manage to fix this? This has become unworkable as user keep closing the browsers without logging in.. I tried your fix, but can't get it to work. the instance.Send, is triggered but nothing happens

TIA

basilkurianmb commented 3 years ago

@JonnySKK The provided solution doesn't work for me also.

basilkurianmb commented 3 years ago

Any updates on this issue?

LarsRadman commented 3 years ago

Same problem here and I cannot get and the solution by @JonnySKK to work. Any news on this?

lukies97 commented 2 years ago

Hey everyone, Not sure if you're still looking for a solution, but I used the above example for Xamarin to get the same behavior as iOS. Instead of `protected override void OnResume() { base.OnResume();

        if (IsAuthenticating)
        {
            ActivityMediator.Instance.Send("com.company.myapp://myauthdomain.eu.auth0.com/android/com.company.myapp/callback");
        }
    }`

I removed the Activity Mediator line and just merely did the logout logic directly in the OnResume method in the MainActivity.

The replacement line for the Activity Mediator looks like this: await Shell.Current.GoToAsync($"//{nameof(InsertPageHerePage)}");

sledderGIT commented 2 years ago

I have the same problem and the solution @JonnySKK doesn't work. I'm getting "Malformed callbackk URL". @lukies97 could you please share a more detailed example of how did you make it work?

redwork321 commented 2 years ago

I also got a "Malformed callback URL". I used this instead, and it works for me:

if (IsAuthenticating)
{
    ActivityMediator.Instance.Cancel();
}
basilkurianmb commented 2 years ago

if (IsAuthenticating) { ActivityMediator.Instance.Send("com.company.myapp://myauthdomain.eu.auth0.com/android/com.company.myapp/callback"); }

This solution doesn't working for me

dTveitan commented 2 years ago

I also got a "Malformed callback URL". I used this instead, and it works for me:

if (IsAuthenticating)
{
    ActivityMediator.Instance.Cancel();
}

This works for me👌

frederikprijck commented 1 year ago

Closing as it looks like there is a workaround available. If not, I would kindly refer to https://github.com/IdentityModel/IdentityModel.OidcClient/issues/54