LoginAsync fails in Dependency Service in Xamarin Forms

Here’s the guidance I need…here’s my method I’m calling the Auth0 login….

    public async Task<string> LoginAuth0Async()
    {
        try
        {
            var auth0Client = new Auth0Client(new Auth0ClientOptions
            {
                Domain = "mycompany.auth0.com",
                ClientId = "myclientId",
                Scope = "openid profile email"
            });

            var user = await auth0Client.LoginAsync(RootView);

//UNHANDLE EXCEPTION HERE - Login Screen launches then app crashes - Null object ref. No other info comes back. I’m SURE it is IN the auth0Client.LoginAsync method and yes, RootView is set and is not null.

            return user.IdentityToken; //never gets to here
        }
        catch(Exception ex) //never gets caught here either
        {
            return null;
        }
    }

As you can see from my comments in the code, I’m hitting an error calling the auth0Client.LoginAsync method. Those comments refer to Droid errors.
On iOS the login screen doesn’t even display and I get “Get method not found for AutomaticallyAdjustScrollViewInsets” error. I have no scrolling on any page, must be what auth0 is trying to display.

I don’t think the auth0Client.LoginAsync() method is handling the RootView/Context well and breaking internally, hence the unhandled exception.

I have similar mechanics in my MainActivity or AppDelegate that launches the login screen on startup and I can login, get token etc. then call the new service.

BUT I need this to work in the dependency SERVICE, not from MainActivity or AppDelegate

ANY help would be great

1 Like

Hey there @lindsaymiles!

Not a Xamarin guy myself unfortunately but will try to help you. Can you tell me what repo of ours do you use? Maybe one of our FAQs will be helpful:

Hi Konrad,
I was using a combination of this:
https://github.com/Daniel-Krzyczkowski/Xamarin/tree/master/XamarinForms/Auth0XamarinForms/Auth0XamarinForms

AND this guide:

https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter2/custom/

And what I’ve realised now is that after the line in the Service fires, it has nothing to return to.
var loginResult= await auth0Client.LoginAsync(RootView)

I have tried to decorate the MainActivity with this:
[IntentFilter(new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataScheme = “com.mycompany.mycompany”,
DataHost = “mycompany.auth0.com”,
DataPathPrefix = “/android/com.mycompany.mycompany/callback”)]

But that brings the return from the .LoginAsync call back to the MainActivity and NOT to where it is called.

Question, how can I get the result to return to here in the Service?
var loginResult = await auth0Client.LoginAsync(RootView)

Anybody?

Thanks

Maybe you can try creating a GitHub issue in the latter repo and the maintainer will suggest you how to tackle that as I’m not sure if Daniel will be able to help as it seems that you’re trying to pass the result from the other repo’s code.

Possibly,

What I’d like to know is if there is a way to either add to this:
var auth0Client = new Auth0Client(new Auth0ClientOptions

  •            {*
    
  •                Domain = "mycompany.auth0.com",*
    
  •                ClientId = "mycompanyId",*
    
  •                Scope = "openid profile email" //"openid profile email" *
    
  •                DataPathPrefix ?,*
    
  •                RedirectUri = ?*
    
  •            });*
    

OR add something like this:
authorizeState = await auth0Client.PrepareLoginAsync();

  •            // Send the user off to the authorization endpoint*
    
  •            var uri = Android.Net.Uri.Parse(authorizeState.StartUrl);*
    
  •            var intent = new Intent(Intent.ActionView, uri);*
    
  •            intent.AddFlags(ActivityFlags.NoHistory);*
    
  •            var loginResult = await auth0Client.LoginAsync(intent)*
    

But the result NEEDS to come back to the service a other actions get performed on the result.

Maybe someone from community will be able to share their insights

Solved.

I implemented Daniel-Krzyczkowski client-side LoginService code into the that created from Adrian Hall’s example/guide.

All works. Both iOS and Android.

1 Like

Ohh thanks a lot for sharing that @lindsaymiles!

Mobile Apps uses the LoginAsync extension method on the MobileServiceClient to sign in a user with App Service authentication.