Edward
Edward

Reputation: 29966

Xamarin.Auth Fail on UWP

I made a test with Xamarin.Auth on uwp, I got error System.NullReferenceException: 'Object reference not set to an instance of an object.' with code below:

            var authenticator = new OAuth2Authenticator(
                        "clientid",
                        "client serrect-@:?",
                        "openid profile",
                        new Uri("https://login.microsoftonline.com/common/oauth2/v2.0/authorize"),
                        new Uri("com.microrookie://oauth2redirect"),
                        new Uri("https://login.microsoftonline.com/common/oauth2/v2.0/token"),
                        null,
                        false);
            authenticator.Completed += Authenticator_Completed;
            authenticator.Error += Authenticator_Error;
            //authenticator.GetUI();
            //Frame.Navigate(authenticator.GetUI(), authenticator);
            var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
            presenter.Login(authenticator); //error throw this line

Upvotes: 1

Views: 248

Answers (2)

Nico Zhu
Nico Zhu

Reputation: 32775

Xamarin.Auth Fail on UWP

The problem is you have not call AuthenticationConfiguration.Init() method in UWP client app.

......

Xamarin.Forms.Forms.Init(e);
global::Xamarin.Auth.Presenters.UWP.AuthenticationConfiguration.Init();

For redirect Url, please refer official document,

If you are using ADAL to build application for your desktop or mobile devices, you may select from the suggested Redirect urls below.

enter image description here

I have checked your redirect uri scheme (microrookie://auth), if you have resisted protocol for your app, it will launch the app after authentication responses successfully.

Upvotes: 1

Douglas
Douglas

Reputation: 11

The null reference is thrown in OAuth2Authenticator.cs for UWP. On Line 790, code calls:

task_scheduler = TaskScheduler.FromCurrentSynchronizationContext();

For UWP this throws an InvalidOperationException (see authors comments in catch handler); since task_scheduler is null, a null reference exception is subsequently thrown.

The fix appears to be simple, juat prefix the offending code with:

if ( SynchronizationContext.Current == null )
    task_scheduler = TaskScheduler.Current;
else ...

I am new to these proceedings, how do I submit my suggestion to the authors?

Upvotes: 1

Related Questions