Authenticating with ADAL and WebAPI without using user credentials

Calling the following code results in a null reference exception. Using Xamarin Forms, can a webAPI call be authenticated without using user credentials and use the client id and the app key? I have no clue as to why Microsofts DLL is throwing this exception

//This code works fine on a native windows app
clientCredential = new ClientCredential(clientId, appKey);
authResult = await authContext.AcquireTokenAsync(serviceResourceId, clientCredential);

throws:
{System.NullReferenceException: Object reference not set to an instance of an object
at Microsoft.IdentityModel.Clients.ActiveDirectory.BrokerHelper.get_CanInvokeBroker () [0x0000c] in :0
at Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase+d__54.MoveNext () [0x0027d] in :0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:201
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:170
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:142
at System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:372 at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext+<AcquireTokenForClientCommonAsync>d__45.MoveNext () [0x00081] in <filename unknown>:0 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:201 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:170 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:142 at System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:372
at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext+d__22.MoveNext () [0x00071] in :0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:201
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:170
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:142
at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:372
at AnnualMeeting.iOS.RootViewController+<>c__DisplayClass42_0+<b__0>d.MoveNext () [0x0005e] in C:\Dev\Annual Meeting\Annual Meeting 3.0\SecureAPI\Source\AnnualMeeting\AnnualMeeting.iOS\RootViewController.cs:161
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.m__0 (System.Object state) [0x00000] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:994
at UIKit.UIKitSynchronizationContext+c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/src/UIKit/UIKitSynchronizationContext.cs:24
at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/src/Foundation/NSAction.cs:163
at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/src/UIKit/UIApplication.cs:77
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/3051/5f11db87/source/maccore/src/UIKit/UIApplication.cs:61

Tagged:

Answers

  • jbravobrjbravobr BRMember ✭✭

    You could try the ADAL library. Look for more here http://www.cloudidentity.com/blog/2015/07/22/using-adal-3-x-with-xamarin-forms/

  • ReidWilliams.7469ReidWilliams.7469 USMember ✭✭

    I am using the ADAL library. I have the code working in the example that you provided. However that is not the intended goal of this post. I dont want the use to have to log in. The code that I posted is also using the ADAL Library and works on a native windows app. I want to know why it does not work on iOS and Xamarin and how can I authenticate using ADAL and the user not have to login.

  • AndyWhitworthAndyWhitworth USMember ✭✭

    Hi Reid,
    Did you manage to resolve this as we are seeing the same issue?
    Regards,
    Andy

  • ReidWilliams.7469ReidWilliams.7469 USMember ✭✭

    We did not. We ended up using it how it was designed to be used, by showing the login screen and logging in when the app is launched. It is definitely not the ideal solution but it worked for us in this case. If we figure it out I will be sure to update this post.

  • kanishkpanwarkanishkpanwar USMember

    I found this issue via SO post. Here is my response from SO question.
    "In adal v3 we have removed the non-interactive authentication for every platform except desktop (only there for legacy reasons). No app should be collecting the creds and should be using the webview to delegate the authentication flow responsibility to AAD. Also, using non-interactive flow will fail when you enable multi-factor authentication."

  • @kanishkpanwar I am using this code to get a token to authenticate a server call:

    var authorityUrl = string.Format(@"https://login.microsoftonline.com/{0}/oauth2/token", AadInstance);
    var context = new AuthenticationContext(authorityUrl);
    var credential = new ClientCredential(ClientId, ClientSecret);
    var authenticationResult = context.AcquireTokenAsync(RemoteClientId, credential).Result;
    return authenticationResult.AccessToken;
    

    But I get this error:

    AcquireTokenHandlerBase.cs: === Token Acquisition started:
        Authority: https://login.microsoftonline.com/f9e55202-63c0-4821-9fc7-e38eb5bc3a08/oauth2/token/
        Resource: 80d147c1-0b9a-48e0-8a62-1dc82890e98e
        ClientId: cab18d6f-3edc-446b-a071-45b28b192f0b
        CacheType: null
        Authentication Target: Client
    
    TokenCache.cs: Looking up cache for a token...
    TokenCache.cs: No matching token was found in the cache
    AcquireTokenHandlerBase.cs: System.NullReferenceException: Object reference not set to an instance of an object
      at Microsoft.IdentityModel.Clients.ActiveDirectory.BrokerHelper.get_CanInvokeBroker () [0x0000c] in <f671779d8b3b49399b31bf519785e86e>:0 
      at Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase+<RunAsync>d__55.MoveNext () [0x00389] in <e4081d9da4634689910019c82c03f3e7>:0
    

    What is going wrong? This code works on the Android version, but not on the iOS.

    I have also asked this question on SO: http://stackoverflow.com/questions/42389764/how-can-i-get-a-token-from-adal-in-xamarin-ios

Sign In or Register to comment.