OneDrive / onedrive-sdk-dotnet-msa-auth-adapter

Other
26 stars 22 forks source link

Microsoft.Graph.ServiceException in UWP app #3

Closed tipa closed 8 years ago

tipa commented 8 years ago

I am trying to authenticate in a UWP app via the following code (app-id X'd out):

var authProvider = new MsaAuthenticationProvider("00000000XXXXXXXX", null, new string[] { "onedrive.appfolder" });
await authProvider.AuthenticateUserAsync();

Unfortunately, the UI to authenticate doesnt appear, instead a ServiceException is raised in AuthenticateUserAsync (Code: authenticationCancelled)

tipa commented 8 years ago

Somehow, it works now...

tipa commented 8 years ago

I was wrong... AuthenticateUserAsync works, when the user already gave the app the permission (with a previous OneDrive SDK version). AuthenticateUserAsyncstill throws an exception when the user did not authorize the app the access OneDrive yet and the popup should be shown. Tested on a desktop having the Anniversary Update.

j-0 commented 8 years ago

I ran into the same issue (Tested on Desktop w/o Anniversary Update) when calling AuthenticateUserAsync. No UI is shown, the exception with Code authenticationCancelled is thrown immediatly. Before updating the OneDrive-SDK Nuget everything worked fine (so no error on app registration part)

cdmayer commented 8 years ago

You must pass in a returnUrl. It must be equal to the value you set up in the App settings.

In the future, you can debug more easily by cloning the auth-adapter code and building a debug binary for use while developing your app. Check out the repo here: https://github.com/OneDrive/onedrive-sample-photobrowser-uwp

tipa commented 8 years ago

Looking at the code, when I pass null as returnUrl, it will be replaced by the ApplicationUrl of the app automatically: https://github.com/OneDrive/onedrive-sdk-dotnet-msa-auth-adapter/blob/21aa5d4354adfc080f9b55b9ca446dfa4205adc4/src/OneDrive.Sdk.Authentication.Common/MsaAuthenticationProvider.cs#L84-L86 Setting the returlUrl to https://login.live.com/oauth20_desktop.srf as it is done in the example you linked to doesnt work either.

Where can I set a ApplicationUrl manually then? Can I find the app settings you are talking about here: https://apps.dev.microsoft.com/#/application/sapi/00000000XXXXXXX? Because under the chapter "mobile application" there is no field I can manually set a value.

cdmayer commented 8 years ago

I see the issue you are having. You are correct that later the returnUrl gets set, however, this gets set to a local URL that doesn't work with the MSA auth flow. I will investigate that separately. Until then, you should be able to get unblocked:

That works locally for me (and I am able to repro your issue locally, too). Please let me know if you get stuck anywhere. If you don't have a remote URI, please paste a screen clipping of what you see (not the whole page, just the Mobile Application section).

tipa commented 8 years ago

As I said in my previous comment I already tried the remote URI without success. Below you find the screen I see in the app settings as well as the code that is not working.

2 unbenannt

ericpapamarcos commented 8 years ago

I'm seeing the same issue. I'm using the same MsaAuthenticationProvider constructor as tipa except I have an additional scope (offline_access).

I have a UWP app (14393 SDK) with a button. Clicking the button fires an event handler, which in turns calls the MsaAuthenticationProvider constructor and AuthenticateAsync.

In my testing I found that the authenticationCancelled exception disappears if the code is not called from an event handler (ex: RoutedEventArgs). For example, if I temporarily call the code from the OnNavigatedTo method, I don't get the exception and the sign in dialog appears.

Is it possible that MsaAuthenticationProvider is not cooperating with event handlers?

cdmayer commented 8 years ago

@tipa Can you post more of the code you're using? I was not able to reproduce the issue you're having.

AK-47ru commented 8 years ago

I found another way to reproduce the exception any time. You can reproduce the error in the https://github.com/OneDrive/onedrive-sample-photobrowser-uwp.

After you press "Login as MSA" and got the Sign In window, closed it without signing. The next time you press login button you'll get the error without the login window appears again.

Just modify the code that is would not navigate to MainPage if not signed in and the program stay on AccountSelection page for another try.

mhomde commented 8 years ago

I think I'm having the same problem. It seems to be working on and off locally but failing in release mode or when installed from the Store. The funny thing is that it works if i disable .Net Native, maybe there's some optimization that screws up JSON serialization or something? Testing it a little more it does fail occasionally even with .Net native disabled but every time with it enabled. Must be some weird timing/ordering bug

Here's a stack trace¨

at Microsoft.OneDrive.Sdk.Authentication.WebAuthenticationBrokerWebAuthenticationUi.d0.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in f:\dd\ndp\fxcore\CoreRT\src\System.Private.CoreLib\src\System\Runtime\ExceptionServices\ExceptionDispatchInfo.cs:line 61 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Threading\src\System\Runtime\CompilerServices\TaskAwaiter.cs:line 187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Threading\src\System\Runtime\CompilerServices\TaskAwaiter.cs:line 156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Threading\src\System\Runtime\CompilerServices\TaskAwaiter.cs:line 128 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Threading\src\System\Runtime\CompilerServices\TaskAwaiter.cs:line 497 at Microsoft.OneDrive.Sdk.Authentication.OAuthHelper.d0.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in f:\dd\ndp\fxcore\CoreRT\src\System.Private.CoreLib\src\System\Runtime\ExceptionServices\ExceptionDispatchInfo.cs:line 61 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Threading\src\System\Runtime\CompilerServices\TaskAwaiter.cs:line 187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Threading\src\System\Runtime\CompilerServices\TaskAwaiter.cs:line 156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Threading\src\System\Runtime\CompilerServices\TaskAwaiter.cs:line 128 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Threading\src\System\Runtime\CompilerServices\TaskAwaiter.cs:line 497 at Microsoft.OneDrive.Sdk.Authentication.MsaAuthenticationProvider.d23.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in f:\dd\ndp\fxcore\CoreRT\src\System.Private.CoreLib\src\System\Runtime\ExceptionServices\ExceptionDispatchInfo.cs:line 61 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Threading\src\System\Runtime\CompilerServices\TaskAwaiter.cs:line 187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Threading\src\System\Runtime\CompilerServices\TaskAwaiter.cs:line 156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Threading\src\System\Runtime\CompilerServices\TaskAwaiter.cs:line 128 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Threading\src\System\Runtime\CompilerServices\TaskAwaiter.cs:line 410 at Microsoft.OneDrive.Sdk.Authentication.MsaAuthenticationProvider.d22.MoveNext()

edandersen commented 8 years ago

+1. This is blocking updating to 2.0.0.

tipa commented 8 years ago

Any updates on this or any ways I can help? This is blocking me updating my app as well.

tenevincent commented 8 years ago

I have the same problem with this api, when trying to debug locally, but when deploying in the mobile emulator it work. I move all the API in a portable assembly outside the UI main assembly.

Have anyone an idea?

cdmayer commented 8 years ago

Still working on this. Will report back when I have updates.

cdmayer commented 8 years ago

New binaries are signed and uploaded to NuGet. Please try installing 1.0.2 of the Auth Adapter and let me know if it fixes your issue.

d32f123 commented 8 years ago

Still does not seem to work for me. Doing the same thing as @ericpapamarcos is doing.

cdmayer commented 8 years ago

I am not able to repro that issue. Can you try adding the following to your event handler (you'll need to make some adjustments based on your actual implementation details):


            try
            {
                result = await authUi.AuthenticateAsync(requestUri, callbackUri);
            }
            catch (ServiceException ex)
            {
                var message = ex.Error.Code + ": " + ex.Error.Message;
                throw;
            }

Then insert a break point at the throw statement and tell me what the value of message is? That will help diagnose the issue.

d32f123 commented 8 years ago

The value of message is: "authenticationFailure: " It does work, if I insert the code into a WPF application for example, though.

tipa commented 8 years ago

I cannot even update to the Nuget version 1.0.2 - "Package restore failed"

d32f123 commented 8 years ago

Well, I've reinstalled Visual Studio with all the newest updates... and for some reason it is all working for me now. Oh, and @tipa, I'm unable to update to 1.0.2 as well, but it works fine after reinstalling with 1.0.1.

tipa commented 8 years ago

I reinstalled the 1.0.1 package, did a full rebuild but still same error @cdmayer : the value of message is "authenticationCancelled: " for me

cdmayer commented 8 years ago

If you are not getting a value in ex.Error.Message then you are still running the old version of the package. You need to double-check that you are on 1.0.2.

tipa commented 8 years ago

As I already stated above, I cannot update to version 1.0.2 on my UWP project. Following error pops up:

Restoring packages for ...\project.json...
  GET https://api.nuget.org/v3-flatcontainer/microsoft.identitymodel.clients.activedirectory/index.json
  OK https://api.nuget.org/v3-flatcontainer/microsoft.identitymodel.clients.activedirectory/index.json 459ms
Package Microsoft.IdentityModel.Clients.ActiveDirectory 1.0.0 is not compatible with uap10.0 (UAP,Version=v10.0). Package Microsoft.IdentityModel.Clients.ActiveDirectory 1.0.0 supports: net40 (.NETFramework,Version=v4.0)
One or more packages are incompatible with UAP,Version=v10.0.
Package Microsoft.IdentityModel.Clients.ActiveDirectory 1.0.0 is not compatible with uap10.0 (UAP,Version=v10.0) / win10-arm. Package Microsoft.IdentityModel.Clients.ActiveDirectory 1.0.0 supports: net40 (.NETFramework,Version=v4.0)
One or more packages are incompatible with UAP,Version=v10.0 (win10-arm).
Package Microsoft.IdentityModel.Clients.ActiveDirectory 1.0.0 is not compatible with uap10.0 (UAP,Version=v10.0) / win10-arm-aot. Package Microsoft.IdentityModel.Clients.ActiveDirectory 1.0.0 supports: net40 (.NETFramework,Version=v4.0)
One or more packages are incompatible with UAP,Version=v10.0 (win10-arm-aot).
Package Microsoft.IdentityModel.Clients.ActiveDirectory 1.0.0 is not compatible with uap10.0 (UAP,Version=v10.0) / win10-x64. Package Microsoft.IdentityModel.Clients.ActiveDirectory 1.0.0 supports: net40 (.NETFramework,Version=v4.0)
One or more packages are incompatible with UAP,Version=v10.0 (win10-x64).
Package Microsoft.IdentityModel.Clients.ActiveDirectory 1.0.0 is not compatible with uap10.0 (UAP,Version=v10.0) / win10-x64-aot. Package Microsoft.IdentityModel.Clients.ActiveDirectory 1.0.0 supports: net40 (.NETFramework,Version=v4.0)
One or more packages are incompatible with UAP,Version=v10.0 (win10-x64-aot).
Package Microsoft.IdentityModel.Clients.ActiveDirectory 1.0.0 is not compatible with uap10.0 (UAP,Version=v10.0) / win10-x86. Package Microsoft.IdentityModel.Clients.ActiveDirectory 1.0.0 supports: net40 (.NETFramework,Version=v4.0)
One or more packages are incompatible with UAP,Version=v10.0 (win10-x86).
Package Microsoft.IdentityModel.Clients.ActiveDirectory 1.0.0 is not compatible with uap10.0 (UAP,Version=v10.0) / win10-x86-aot. Package Microsoft.IdentityModel.Clients.ActiveDirectory 1.0.0 supports: net40 (.NETFramework,Version=v4.0)
One or more packages are incompatible with UAP,Version=v10.0 (win10-x86-aot).
Package restore failed for 'MyApp'.
Package restore failed. Rolling back package changes for 'MyApp'.
========== Finished ==========
Time Elapsed: 00:00:02.5641889
cdmayer commented 8 years ago

@tipa You have pointed out a different issue, thank you for doing so. The .nuspec file does not have the correct package version constraints. Try first upgrading Microsoft.IdentityModel.Clients.ActiveDirectory to a newer version, such as 2.28. It must be >2.21 and <3.0. Then, install 1.0.2 of this package. In the meantime I need to update the package requirements.

cdmayer commented 8 years ago

Updated to 1.0.3. Same binaries, updated package requirements.

cdmayer commented 8 years ago

@tipa please let me know if you are still having issues with 1.0.3. I would not expect you to be getting quite the same errors with that version.

tipa commented 8 years ago

I can update it now but the issue is still not solved for me. Now the error message is the following:

The provided value for the input parameter 'redirect_uri' is not valid. The client application must be marked as 'mobile', or the value must be an absolute URL which matches the registered redirect URI.

No matter if I use var authProvider = new MsaAuthenticationProvider("00000000XXXXX", null, new string[] { "onedrive.readonly" }); or var authProvider = new MsaAuthenticationProvider("00000000XXXXX", "https://login.live.com/oauth20_desktop.srf", new string[] { "onedrive.readonly" });

cdmayer commented 8 years ago

@tipa the good news is that the error message you're getting means that we have solved the authenticationCancelled problem! Hooray!

Usually the error message you're getting is caused by application settings not matching the inputs you provide. Can you double-check that you have the correct ApplicationId set? If that still doesn't work, I think we need to check and see if the issue truly lies in the SDK or not. Try logging in using a web request tool like Fiddler and the instructions here.

tipa commented 8 years ago

It is working now, thanks for your patience!

cdmayer commented 8 years ago

@tipa glad we got it solved :100:

tenevincent commented 8 years ago

I have updated the assembly "Microsoft.OneDrive.Sdk.Authentication" to 1.0.3 and it is working now,

thank you for the update!

AK-47ru commented 8 years ago

Works like a charm on 1.0.3, many thanks.