1

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

Edward
  • 28,296
  • 11
  • 76
  • 121
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Pavel Anikhouski Jan 04 '20 at 08:10
  • @PavelAnikhouski Have you made a test with above code? What do you think your link is related? I have initialize `authenticator` and `authenticator`, where did you think it is null? – Edward Jan 04 '20 at 11:28

2 Answers2

1

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?

Io-oI
  • 2,514
  • 3
  • 22
  • 29
Douglas
  • 11
  • 1
  • Could you share how to debug Xamarin.Auth? – Edward Jan 05 '20 at 11:14
  • 1. Access the nuget site for the Xamarin.Auth project. Download or clone the repository. Now create your own UWP solution and add the project for Xamarin.Auth.UniversalWindowsPlatform – Douglas Jan 05 '20 at 15:17
  • Access the nuget site for the Xamarin.Auth project. Download or clone the repository. Now create your own UWP solution and add the project for Xamarin.Auth.UniversalWindowsPlatform You can now add the original poster's code for authentication and place a breakpoint on the line that throws the exception. You can now step into the Xamarin.Auth code and will find the exception that I stated above. Can anyone point me to a reference as to how I can recompile the updated code so I can use it as a nuget package in my projects? I am sure this must be easy but I've never done this before! – Douglas Jan 05 '20 at 15:26
  • If you want to constribute to the Xamarin.Auth, you need to create pull request to Xamarin.Auth. If you just want to use your library by yoursefl, you could create your own nuget package with your project. – Edward Jan 06 '20 at 00:27
  • 1
    @Edward, Have you called [`AuthenticationConfiguration.Init()`](https://github.com/xamarin/Xamarin.Auth/blob/master/samples/Xamarin.Forms/someone1984-Xamarin.Auth-sample/AuthExample/AuthExample/AuthExample.UWP/App.xaml.cs#L57) method in uwp client project? – Nico Zhu Jan 06 '20 at 07:27
  • @NicoZhu-MSFT After calling Init, it resolves this error, but keep the [scrren](https://i.stack.imgur.com/BqUNF.png) all the time. – Edward Jan 06 '20 at 08:29
  • Please check this [document](https://github.com/xamarin/Xamarin.Auth#0-server-side). – Nico Zhu Jan 06 '20 at 08:42
  • @NicoZhu-MSFT I configured `microrookie://auth` at uwp and azure web register, is there any issue with this url? – Edward Jan 06 '20 at 08:46
  • *Server/Web app uses http[s] schemes for redirect_url, because it loads real web page (url-authority can be localhost or real hostname like http://xamarin.com).* – Nico Zhu Jan 06 '20 at 08:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/205416/discussion-between-edward-and-nico-zhu-msft). – Edward Jan 06 '20 at 08:51
  • @NicoZhu-MSFT It seems as the suggestion from Douglas, we need to add code below ` task_scheduler = TaskScheduler.Current;` when `task_scheduler` is null. – Edward Jan 06 '20 at 09:23
  • @NicoZhu-MSFT can you reproduce this issue? I still think the redirect_url should not be https. – Edward Jan 06 '20 at 12:09
1

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.

Community
  • 1
  • 1
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • As in the discussion, I got https://i.stack.imgur.com/BqUNF.png with `microrookie://auth`, do you have a working demo? – Edward Jan 06 '20 at 12:38
  • Do you resisted `microrookie://auth` protocol for the app? – Nico Zhu Jan 06 '20 at 12:57
  • you only could register with `microrookie`. `://` is invalid in ` – Edward Jan 06 '20 at 13:01
  • what happen when you click Yes in switch window? – Nico Zhu Jan 06 '20 at 13:04
  • What is your test result for xamarin.auth? – Edward Jan 06 '20 at 13:43
  • I'm sorry to tell you, I have no permission to create azure apps. – Nico Zhu Jan 06 '20 at 13:45
  • I had the same problem of nullreference exception in UWP on this line: presenter.Login(auth) Based on the project https://github.com/stevenchang0529/XamarinGoogleDriveRest. Adding "global::Xamarin.Auth.Presenters.UWP.AuthenticationConfiguration.Init()" in App.xaml.cs :: OnLanched() of the UWP project solves the problem. It seems to be forgotten in the repo. – Belight Nov 21 '21 at 12:23