xamarin / Xamarin.Auth

Xamarin.Auth
Apache License 2.0
541 stars 351 forks source link

UWP with grand type Authorization Code doesn't work #308

Open msadeh opened 6 years ago

msadeh commented 6 years ago

Xamarin.Auth Issue

In UWP because of this error the entire code is not working.

try {
task_scheduler = TaskScheduler.FromCurrentSynchronizationContext(); } catch (System.InvalidOperationException exc_io) { / TODO: INVESTIGATE UWP only exception System.InvalidOperationException: The current SynchronizationContext may not be used as a TaskScheduler / string msg = exc_io.Message; System.Diagnostics.Debug.WriteLine($"OAuthAuthenticator exception {msg}"); }

Version

Steps to reproduce

1. 2. 3.

Platform:

Expected behaviour

task_scheduler is NOT null the this code should run.

RequestAccessTokenAsync(code) .ContinueWith ( task => { if (task.IsFaulted) { OnError(task.Exception); } else { OnRetrievedAccountProperties(task.Result); } }, task_scheduler );

Actual behaviour

because task_scheduler is null, RequestAccessTokenAsync is not running. Tell us what happens instead Can you also include a screen shot?

IF IT IS A NEW FEATURE REQUEST, INCLUDE THIS PART:

Feature description

Write a description of the feature. How should it work? How should it look? Include some graphics if this could help!

newky2k commented 6 years ago

@msadeh could you please provide a small sample to replicate the issue for us to investigate

msadeh commented 6 years ago

Hey,

Because the exception in UWP task_scheduler is null. so the reset of the code is not working.

Current code in OAuth2Authenticator:

TaskScheduler task_scheduler = null; try { task_scheduler = TaskScheduler.FromCurrentSynchronizationContext(); } catch (System.InvalidOperationException exc_io) { / TODO: INVESTIGATE UWP only exception System.InvalidOperationException: The current SynchronizationContext may not be used as a TaskScheduler / string msg = exc_io.Message; System.Diagnostics.Debug.WriteLine($"OAuthAuthenticator exception {msg}"); } I think adding this line in the catch should be a work around: _taskscheduler = TaskScheduler.Current;

catch (System.InvalidOperationException exc_io) { / TODO: INVESTIGATE UWP only exception System.InvalidOperationException: The current SynchronizationContext may not be used as a TaskScheduler / task_scheduler = TaskScheduler.Current; string msg = exc_io.Message; System.Diagnostics.Debug.WriteLine($"OAuthAuthenticator exception {msg}"); }

Thanks, Mano

Redth commented 5 years ago

Are you calling this on the UI Thread? Seems like a UI Thread related issue.

LarsWeg commented 5 years ago

This is still an issue with UWP and seems not related to UI thread. OnRedirectPageLoaded comes up in a new thread that has no SynchronizationContext assigned. The solution @msadeh proposed fixes it. Is there a chance this could make it into one of the next releases?

MichaelPeter commented 4 years ago

Is there an update on this? Using OneDrive Personal on Xamarin.Forms on UWP where this crashes my code.

The error is: The current SynchronizationContext may not be used as a TaskScheduler.

Could you at least fix the part if task_sceduler is null that the ContinueWith is called without a null exception. Code is only possible to debug with decompilation since the Diagnostics.WriteLine is not in productive code. Or make the try catch code call OnError and not eat silently the error? This is bad coding practice and can only be fixed if the developer looks in the source code.

I had to override OnRedirectPageLoaded to find out about the error, this bug cost me a good 2 hours to find.