# LoginAsync fails in Dependency Service in Xamarin Forms

**URL:** <https://community.auth0.com/t/loginasync-fails-in-dependency-service-in-xamarin-forms/40592>\
**Category:** Get Help\
**Tags:** xamarin\
**Created:** [April 6, 2020, 5:36pm UTC](https://community.auth0.com/t/loginasync-fails-in-dependency-service-in-xamarin-forms/40592 "2020-04-06T17:36:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lindsaymiles](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lindsaymiles/32/12931_2.png) [@lindsaymiles](https://community.auth0.com/u/lindsaymiles)\
**Post date:** [April 6, 2020, 5:36pm UTC](https://community.auth0.com/t/loginasync-fails-in-dependency-service-in-xamarin-forms/40592/1 "2020-04-06T17:36:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [April 7, 2020, 9:11am UTC](https://community.auth0.com/t/loginasync-fails-in-dependency-service-in-xamarin-forms/40592/2 "2020-04-07T09:11:49Z")

</div>

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:

[http://community.auth0.com/t/i-follow-your-docs-quickstart-and-i-have-a-problem-but-it-has-a-github-repo-what-do-i-do-now/39519](http://community.auth0.com/t/i-follow-your-docs-quickstart-and-i-have-a-problem-but-it-has-a-github-repo-what-do-i-do-now/39519)

---

<div class="post-metadata">

**Author:** ![lindsaymiles](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lindsaymiles/32/12931_2.png) [@lindsaymiles](https://community.auth0.com/u/lindsaymiles)\
**Post date:** [April 7, 2020, 12:13pm UTC](https://community.auth0.com/t/loginasync-fails-in-dependency-service-in-xamarin-forms/40592/3 "2020-04-07T12:13:43Z")

</div>

Hi Konrad,  
I was using a combination of this:  
[https://github.com/Daniel-Krzyczkowski/Xamarin/tree/master/XamarinForms/Auth0XamarinForms/Auth0XamarinForms](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/](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](http://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

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [April 7, 2020, 12:22pm UTC](https://community.auth0.com/t/loginasync-fails-in-dependency-service-in-xamarin-forms/40592/4 "2020-04-07T12:22:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lindsaymiles](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lindsaymiles/32/12931_2.png) [@lindsaymiles](https://community.auth0.com/u/lindsaymiles)\
**Post date:** [April 7, 2020, 12:30pm UTC](https://community.auth0.com/t/loginasync-fails-in-dependency-service-in-xamarin-forms/40592/5 "2020-04-07T12:30:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [April 7, 2020, 12:31pm UTC](https://community.auth0.com/t/loginasync-fails-in-dependency-service-in-xamarin-forms/40592/6 "2020-04-07T12:31:54Z")

</div>

Maybe someone from community will be able to share their insights

---

<div class="post-metadata">

**Author:** ![lindsaymiles](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lindsaymiles/32/12931_2.png) [@lindsaymiles](https://community.auth0.com/u/lindsaymiles)\
**Post date:** [April 10, 2020, 2:59pm UTC](https://community.auth0.com/t/loginasync-fails-in-dependency-service-in-xamarin-forms/40592/7 "2020-04-10T14:59:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [April 14, 2020, 8:23am UTC](https://community.auth0.com/t/loginasync-fails-in-dependency-service-in-xamarin-forms/40592/8 "2020-04-14T08:23:50Z")

</div>

Ohh thanks a lot for sharing that @lindsaymiles!

---

<div class="post-metadata">

**Author:** ![scottkche](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/scottkche/32/12829_2.png) [@scottkche](https://community.auth0.com/u/scottkche)\
**Post date:** [April 14, 2020, 10:52am UTC](https://community.auth0.com/t/loginasync-fails-in-dependency-service-in-xamarin-forms/40592/9 "2020-04-14T10:52:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [April 14, 2020, 12:21pm UTC](https://community.auth0.com/t/loginasync-fails-in-dependency-service-in-xamarin-forms/40592/10 "2020-04-14T12:21:45Z")

</div>


