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

[Bug] MSAL not working with MAUI in iOS #3093

Closed
1 of 7 tasks
chrfin opened this issue Jan 7, 2022 · 9 comments
Closed
1 of 7 tasks

[Bug] MSAL not working with MAUI in iOS #3093

chrfin opened this issue Jan 7, 2022 · 9 comments
Assignees

Comments

@chrfin
Copy link

chrfin commented Jan 7, 2022

Logs and network traces
https://pastebin.com/B7ux36cs (anonymized)
and then
https://pastebin.com/1tJTqDWE

Which version of MSAL.NET are you using?
Microsoft.Identity.Client 4.39.0

Platform
.NET MAUI preview 11 running on iOS

What authentication flow has the issue?

  • Desktop / Mobile
    • Interactive
    • Integrated Windows Authentication
    • Username Password
    • Device code flow (browserless)
  • Web app
    • Authorization code
    • On-Behalf-Of
  • Daemon app
    • Service to Service calls

Is this a new or existing app?
This is a new app, but the same code does work in Xamarin.Forms/.iOS.

Repro

Create a .NET MAUI app from the default template, add MSAL and try to log in with AAD B2C.
I use the following code to bootstrap MSAL:

AuthClient = PublicClientApplicationBuilder.Create(AuthConstants.ClientId)
		.WithIosKeychainSecurityGroup(AuthConstants.IosKeychainSecurityGroups)
		.WithB2CAuthority($"{AuthConstants.Authority}{AuthConstants.SigninPolicy}")
		.WithRedirectUri($"msal{AuthConstants.ClientId}://auth")
		.Build();

and

var result = await AuthClient
				.AcquireTokenInteractive(AuthConstants.DefaultScopes)
				.WithPrompt(Prompt.SelectAccount)
				.WithUseEmbeddedWebView(true)
				.ExecuteAsync().ConfigureAwait(false);

Expected behavior
To get a AuthenticationResult like on Windows/Android.

Actual behavior
Browser is closed and the debug log logs the attached exception and no user is logged in.

Additional context / logs / screenshots / links to code

@SameerK-MSFT
Copy link
Contributor

We are not supporting MAUI yet. Marked as Bug. So will keep on the radar.

@chrfin
Copy link
Author

chrfin commented Jan 7, 2022

Any ETA for official support?

@bgavrilMS
Copy link
Member

bgavrilMS commented Jan 10, 2022

@chrfin - what is the target framework that you used ? This seems like an HttpClient bug. We inject an NsUrl based HttpClient (code here) as per Xamarin recommendations. Would it be possible to try to use a vanilla HttpClient and see if it works? You can do it via WithHttpClientFactory - just create a static HttpClient and return it.

@SameerK-MSFT, @trwalke @pmaytak - I thought we tested Android and iOS with MAUI and it worked.

@chrfin
Copy link
Author

chrfin commented Jan 10, 2022

@bgavrilMS
I'm targeting net6.0-ios using .NET MAUI preview 11.
With a new/vanilla HttpClient supplied via WithHttpClientFactory it seems to be working for that problem, but I'm getting new errors, e.g.:

[0:] Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSRangeException Reason: *** -[__NSArrayM objectAtIndex:]: index 4614066048 beyond bounds [0 .. 0]
Native stack trace:
0   CoreFoundation                      0x00007fff203feba4 __exceptionPreprocess + 242
1   libobjc.A.dylib                     0x00007fff201a1be7 objc_exception_throw + 48
2   CoreFoundation                      0x00007fff2047ef64 _CFThrowFormattedException + 194
3   CoreFoundation                      0x00007fff202fa29b -[__NSArrayM getObjects:range:] + 0
4   libxamarin-dotnet-debug.dylib       0x000000010b9406f9 xamarin_dyn_objc_msgSend + 217
5   libmonosgen-2.0.dylib               0x000000010ca43ac4 do_icall + 276
6   libmonosgen-2.0.dylib               0x000000010ca42a8d do_icall_wrapper + 253
7   libmonosgen-2.0.dylib               0x000000010ca34caa interp_exec_method + 2970
8   libmonosgen-2.0.dylib               0x000000010ca32f2f interp_runtime_invoke + 239
9   libmonosgen-2.0.dylib               0x000000010c92acbb mono_jit_runtime_invoke + 1227
10  libmonosgen-2.0.dylib               0x000000010c84d438 mono_runtime_invoke_checked + 136
11  libmonosgen-2.0.dylib               0x000000010c865a55 start_wrapper_internal + 613
12  libmonosgen-2.0.dylib               0x000000010c8657ce start_wrapper + 62
13  libsystem_pthread.dylib             0x00007fff6da324f4 _pthread_start + 125
14  libsystem_pthread.dylib             0x00007fff6da2e00f thread_start + 15

  at ObjCRuntime.Runtime.ThrowNSException(IntPtr ns_exception)
  at ObjCRuntime.Runtime.throw_ns_exception(IntPtr exc)
  at Foundation.NSArray.GetAtIndex(IntPtr handle, nuint i)
  at Foundation.NSArray.ArrayFromHandleFunc[SecRecord](IntPtr handle, Func`2 createObject)
  at Security.SecKeyChain.QueryAsRecord(SecRecord query, Int32 max, SecStatusCode& result)
  at Microsoft.Identity.Client.Platforms.iOS.iOSTokenCacheAccessor.GetPayloadAsString(Int32 type)
  at Microsoft.Identity.Client.Platforms.iOS.iOSTokenCacheAccessor.GetAllRefreshTokens(String optionalPartitionKey)
  at Microsoft.Identity.Client.TokenCache.GetAllRefreshTokensWithNoLocks(Boolean filterByClientId, String partitionKey)
  at Microsoft.Identity.Client.TokenCache.Microsoft.Identity.Client.ITokenCacheInternal.GetAccountsAsync(AuthenticationRequestParameters requestParameters)
  at Foundation.NSArray.GetAtIndex(IntPtr handle, nuint i)
  at Foundation.NSArray.ArrayFromHandleFunc[SecRecord](IntPtr handle, Func`2 createObject)
  at Security.SecKeyChain.QueryAsRecord(SecRecord query, Int32 max, SecStatusCode& result)
  at Microsoft.Identity.Client.Platforms.iOS.iOSTokenCacheAccessor.GetPayloadAsString(Int32 type)
  at Microsoft.Identity.Client.Platforms.iOS.iOSTokenCacheAccessor.GetAllRefreshTokens(String optionalPartitionKey)
  at Microsoft.Identity.Client.TokenCache.GetAllRefreshTokensWithNoLocks(Boolean filterByClientId, String partitionKey)
  at Microsoft.Identity.Client.TokenCache.Microsoft.Identity.Client.ITokenCacheInternal.GetAccountsAsync(AuthenticationRequestParameters requestParameters)
  at Microsoft.Identity.Client.Cache.CacheSessionManager.GetAccountsAsync()
  at Microsoft.Identity.Client.ClientApplicationBase.GetAccountsInternalAsync(ApiIds apiId, String homeAccountIdFilter, CancellationToken cancellationToken)
  at Microsoft.Identity.Client.ClientApplicationBase.GetAccountsAsync(CancellationToken cancellationToken)
  at Microsoft.Identity.Client.ClientApplicationBase.GetAccountsAsync()
  at MyAPP.Authentication.Auth.GetTokenSilentAsync(String scope) 0   CoreFoundation                      0x00007fff203feba4 __exceptionPreprocess + 242
1   libobjc.A.dylib                     0x00007fff201a1be7 objc_exception_throw + 48
2   CoreFoundation                      0x00007fff2047ef64 _CFThrowFormattedException + 194
3   CoreFoundation                      0x00007fff202fa29b -[__NSArrayM getObjects:range:] + 0
4   libxamarin-dotnet-debug.dylib       0x000000010b9406f9 xamarin_dyn_objc_msgSend + 217
5   libmonosgen-2.0.dylib               0x000000010ca43ac4 do_icall + 276
6   libmonosgen-2.0.dylib               0x000000010ca42a8d do_icall_wrapper + 253
7   libmonosgen-2.0.dylib               0x000000010ca34caa interp_exec_method + 2970
8   libmonosgen-2.0.dylib               0x000000010ca32f2f interp_runtime_invoke + 239
9   libmonosgen-2.0.dylib               0x000000010c92acbb mono_jit_runtime_invoke + 1227
10  libmonosgen-2.0.dylib               0x000000010c84d438 mono_runtime_invoke_checked + 136
11  libmonosgen-2.0.dylib               0x000000010c865a55 start_wrapper_internal + 613
12  libmonosgen-2.0.dylib               0x000000010c8657ce start_wrapper + 62
13  libsystem_pthread.dylib             0x00007fff6da324f4 _pthread_start + 125
14  libsystem_pthread.dylib             0x00007fff6da2e00f thread_start + 15

@chrfin
Copy link
Author

chrfin commented Jan 21, 2022

@bgavrilMS I just updated to MAUI preview 12 and now it does not compile anymore, as it does not find `AuthenticationContinuationHelper`.

I guess this is because MAUI does not support the Xamarin.iOS TFMs anymore (see here) and uses another more common one, which does not have this class.

@chrfin
Copy link
Author

chrfin commented Feb 17, 2022

@bgavrilMS Any update on this? v4.41 still has no net6.0-ios TFM... 😢

@bgavrilMS
Copy link
Member

bgavrilMS commented Feb 17, 2022

No, we are not working on this right now. We are waiting for a the net6.0-ios breaking changes to stabilize first. We will pick this up in a few weeks. Our goal is to get all of this working by May.

That said, please feel free to contribute to MSAL if you understand how to fix it, and we will prioritize reviewing the PR.

@chrfin
Copy link
Author

chrfin commented Feb 17, 2022

Thank you for the update. I will finish the Android version of the app first to get something out to testers and then look into iOS again and see if I'm able to contribute something 😊.

@bgavrilMS
Copy link
Member

Closing as duplicate of #3127

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

3 participants