Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException on PublicClientApplication.AcquireTokenAsync() #617

Closed
brunokc opened this issue Sep 24, 2018 · 16 comments
Closed

NullReferenceException on PublicClientApplication.AcquireTokenAsync() #617

brunokc opened this issue Sep 24, 2018 · 16 comments

Comments

@brunokc
Copy link

brunokc commented Sep 24, 2018

Which Version of MSAL are you using ?
MSAL 2.0.1-preview

Which platform has the issue?
Xamarin iOS

What authentication flow has the issue?
iPhone with iOS 12.

Repro

    string[] MsalScopes = { "User.Read" };
    AuthenticationResult authResult = null;
    var app = App.PublicClientApplication;

    try
    {
        var users = await app.GetAccountsAsync();
        authResult = await app.AcquireTokenSilentAsync(MsalScopes, users.FirstOrDefault());
    }
    catch (MsalUiRequiredException e)
    {
        authResult = await app.AcquireTokenAsync(MsalScopes);
    }

Expected behavior
AcquireTokenAsync() should complete successfully and start the client authentication flow on the phone. This same code snipped worked just fine with MSAL 1.1.4-preview0002.

Note that AcquireTokenSilentAsync() works as expected, throwing a MsalUiRequiredException because users.Count is zero.

Actual behavior
The call into AcquireTokenAsync(MsalScopes) throws a NullReferenceException.

Additional context/ Logs / Screenshots
Stack trace when the exception is thrown:

  at Microsoft.Identity.Client.PublicClientApplication+<AcquireTokenForLoginHintCommonAsync>d__22.MoveNext () [0x0003a] in <707458e4f41141b3b1a5c88f7800ecf3>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <7d5a05cfcb09432d8cc656b9d781e54b>:0 
  at Microsoft.Identity.Client.PublicClientApplication+<AcquireTokenAsync>d__7.MoveNext () [0x00096] in <707458e4f41141b3b1a5c88f7800ecf3>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <7d5a05cfcb09432d8cc656b9d781e54b>:0 
  at Project1.iOS.Services.LoginProvider+<LoginMicrosoftAccountAsync>d__9.MoveNext () [0x001d2] in C:\Users\<user>\OneDrive\Projects\Project1\client\Project1.iOS\Services\LoginProvider.cs:83 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <7d5a05cfcb09432d8cc656b9d781e54b>:0 
  at Project1.iOS.Services.LoginProvider+<LoginAsync>d__13.MoveNext () [0x00088] in C:\Users\<user>\OneDrive\Projects\Project1\client\Project1.iOS\Services\LoginProvider.cs:186 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <7d5a05cfcb09432d8cc656b9d781e54b>:0 
  at Project1.Views.MainPage+<loginProvider>d__6.MoveNext () [0x00050] in C:\Users\<user>\OneDrive\Projects\Project1\client\Project1\Views\MainPage.xaml.cs:61 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 
  at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/Foundation/NSAction.cs:178 
  at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/UIKit/UIApplication.cs:79 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0002c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/UIKit/UIApplication.cs:63 
  at Project1.iOS.Application.Main (System.String[] args) [0x00002] in C:\Users\<user>\OneDrive\Projects\Project1\client\Project1.iOS\Main.cs:19 

[edit: removed 'other issues' -- it was empty.]

@sergioacortes
Copy link

Same problem, I have tested it with emulator and physical device, I have downgrade to version 1.1.4 preview and it works.

Guess it is a problem within the constructor of PublicClientApplication class

Regards

@brunokc
Copy link
Author

brunokc commented Sep 24, 2018

Thank you for confirming, @sergioacortes. To be clear though, in my case I can construct the class just fine. I set its RedirectUri property and only when I call AcquireTokenAsync() I get the exception. I have also downgraded to 1.1.4-preview0002 and things are working again.

@jennyf19
Copy link
Collaborator

@sergioacortes @brunokc

We are aware of the issue w/iOS simulator. Recommendation is to continue using 1.1.4-preview for the time being.

@sergioacortes
Copy link

@brunokc @jennyf19 I have downgrade to version 1.1.4 It works well but not with physical devices with iOS 12

Kind regards

@brunokc
Copy link
Author

brunokc commented Sep 27, 2018

@sergioacortes, version 1.1.4 seems to be working fine for me on my iPhone with iOS 12. I can't use the resulting token to authenticate with my Azure Mobile Services but I believe that's a problem there (it doesn't look like it supports RS256 JWTs?). The resulting user returned by MSAL seems to be all correct, as far as I can tell.

@jennyf19
Copy link
Collaborator

jennyf19 commented Oct 3, 2018

@brunokc @sergioacortes

Can you try the following, using MSALv2.1.0-preview:

  • In VS, under Debug tab, go to nameOfMyApp.iOS Properties...
  • Then go to iOS Bundle Signing
  • Under Custom Entitlements, click the ... and select the Entitlements.plist file from your app

In the csproj file of the iOS app, you should have this line now included:
<CodesignEntitlements>Entitlements.plist</CodesignEntitlements>
Also, in Entitlements.plist file, make sure you have enabled access to the keychain, using either the below access group or your own:

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>keychain-access-groups</key>
  <array>
    <string>$(AppIdentifierPrefix)com.microsoft.adalcache</string>
  </array>
</dict>
</plist>

Remember to rebuild. Let me know if this works for you. It worked for me on a new iOS app and on our V2 sample.
cc: @jmprieur

@brunokc
Copy link
Author

brunokc commented Oct 4, 2018

Update: it looks like I had forgotten to add the Entitlements for the debug version of my app, as you had requested. I did and now I can't run. I have a free Apple developer account, so there are things I can't do just yet. Unless you can drop the keychain-access-groups requirement, I'm afraid I can't validate the fix for you.


@jennyf19 , unfortunately it didn't seem to work for me. Using 2.1.0-preview, made sure I had keychain-access-groups in my Entitlements list (I already had it in there for other reasons) using my own access group, tried that same code and got the same NullReferenceException.

  at Microsoft.Identity.Client.PublicClientApplication+<AcquireTokenForLoginHintCommonAsync>d__22.MoveNext () [0x0003a] in <2a97cd931f1242d58d9f88cc99534e1d>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <7d5a05cfcb09432d8cc656b9d781e54b>:0 
  at Microsoft.Identity.Client.PublicClientApplication+<AcquireTokenAsync>d__7.MoveNext () [0x00096] in <2a97cd931f1242d58d9f88cc99534e1d>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <7d5a05cfcb09432d8cc656b9d781e54b>:0 
  at Project.iOS.Services.LoginProvider+<LoginMicrosoftAccountAsync>d__9.MoveNext () [0x001c8] in C:\Users\<user>\OneDrive\Projects\Project\client\Project.iOS\Services\LoginProvider.cs:83 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <7d5a05cfcb09432d8cc656b9d781e54b>:0 
  at Project.iOS.Services.LoginProvider+<LoginAsync>d__13.MoveNext () [0x00088] in C:\Users\<user>\OneDrive\Projects\Project\client\Project.iOS\Services\LoginProvider.cs:185 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <7d5a05cfcb09432d8cc656b9d781e54b>:0 
  at Project.Views.MainPage+<loginProvider>d__6.MoveNext () [0x00050] in C:\Users\<user>\OneDrive\Projects\Project\client\Project\Views\MainPage.xaml.cs:61 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 
  at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/Foundation/NSAction.cs:178 
  at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/UIKit/UIApplication.cs:79 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0002c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/UIKit/UIApplication.cs:63 
  at Project.iOS.Application.Main (System.String[] args) [0x00002] in 

Again, this was tested on a real device (iPhone 6S with iOS 12).

Let me know if I can provide any additional information.

@jennyf19
Copy link
Collaborator

jennyf19 commented Oct 4, 2018

Thanks @brunokc - do you have this in the csproj file of the iOS app?<CodesignEntitlements>Entitlements.plist</CodesignEntitlements>

@brunokc
Copy link
Author

brunokc commented Oct 4, 2018

I do. At first it was only under Release|iPhone. Following your instructions, I added it under Debug|iPhone as well. That's when deployment stopped working, probably due to my limited (free) developer account I'm using.

@jennyf19
Copy link
Collaborator

jennyf19 commented Oct 5, 2018

@brunokc
Returning error message in MSAL v2.2.0 and updates to wiki for configuration assistance

Can you try w/v2.2.0? if you're still having issues, email me (jeferrie@microsoft.com) and we'll go from there.

@jennyf19 jennyf19 added this to the 2.2.0 milestone Oct 5, 2018
@jennyf19
Copy link
Collaborator

jennyf19 commented Oct 8, 2018

Fixed w/MSALv2.2.0 and update to Wiki w/clearer advice for iOS UIParent and fixing of incorrect link to Xamarin-v2 sample. FYI - @brunokc
cc: @jmprieur

@tudor06
Copy link

tudor06 commented Nov 6, 2018

I am getting the exact same error using Microsoft.Identity.Client 2.3.1 - same as @brunokc, things are working very well with 1.1.4.
Have ensured that keychain is enabled and the Entitlement list is correctly set.

Any ideas?

@jennyf19
Copy link
Collaborator

jennyf19 commented Nov 6, 2018

@tudor06 - Have you tried restarting the mac and VS, and disconnecting/reconnecting the pairing to the mac. Also, you could try uninstalling the app, clearing the cache completely.
Please send the stack trace and logs, and we'll take a look. thanks.

@brunokc
Copy link
Author

brunokc commented Nov 6, 2018

@tudor06, one key difference for me between 1.1.4 and the current releases was the need to specify an UIParent parameter to the call to AcquireTokenAsync. In other words, as far I can tell, calling AcquireTokenAsync with just the scopes arguments is not supported anymore on iOS. You need to use the one that takes the scopes and an UIParent instance, like so:

 authResult = await app.AcquireTokenAsync(MsalScopes, App.UIParent);

See if that helps you.

@jennyf19
Copy link
Collaborator

jennyf19 commented Nov 6, 2018

@tudor06 there's a discussion on the UIParent if you want to give feedback or suggestions.

@tudor06
Copy link

tudor06 commented Nov 7, 2018

@brunokc : Thank you! That worked!
@jennyf19 : I think it is Ok to keep the issue closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants