[Bug] Cannot acquire token in UWP app on HoloLens via a unity plugin as json serialization fails
See original GitHub issueWhich Version of MSAL are you using ? MSAL 4.5.1
Platform UWP, HoloLens 2, ARM
What authentication flow has the issue?
- Desktop / Mobile
- Interactive
- Integrated Windows Auth
- Username Password
- Device code flow (browserless)
Other? - please describe;
Is this a new or existing app? Trying to migrate app from HoloLens 1 to HoloLens 2
Repro
try
{
_authResult = await app.AcquireTokenSilent(scopes, account).ExecuteAsync();
}
catch (MsalUiRequiredException)
{
try
{
_authResult = await app.AcquireTokenInteractive(scopes).ExecuteAsync();
}
catch (MsalException msalex)
{
Debug.Log($"Error Acquiring Token: {msalex.Message}");
return _authResult;
}
}
Expected behavior Authentication will succeed.
Actual behavior “IL2CPP does not support marshaling delegates that point to instance methods to native code” exception is thrown
Possible Solution Allow to use different JSON serializer.
Additional context/ Logs / Screenshots Hello! I’m trying to use MSAL 4.5.1 in application build in Unity 2018.4 (using IL2CPP scripting backend) for HoloLens 2 (UWP, ARM). I used “UWP standalone” project, compiled it for ARM and I imported library as plugin in Unity.
Previously, I was successfully using MSAL library in app build in Unity with .Net scripting backend for HoloLens 1 (UWP, x86).
In my code, as above, I’m trying to acquire token silently first and then acquire token in interactive mode. During first app run I’m getting MsalUiRequiredException exception, what is for my understanding correct and then AcquireTokenInteractive() is called.
But problem occurs here - I’m getting this exception “IL2CPP does not support marshaling delegates that point to instance methods to native code”
Thanks to build in Unity 2019.2 I’m getting more detailed info here:
System.NotSupportedException: IL2CPP does not support marshaling delegates that point to instance methods to native code. The method we’re attempting to marshal is: System.Runtime.Diagnostics.DiagnosticsEventProvider::EtwEnableCallBack
The point is that JsonHelper class is using DataContractSerializer.ReadObject method:
...
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject (System.IO.Stream stream) ...
at Microsoft.Identity.Client.Utils.JsonHelper.DeserializeFromJson[T] (System.Byte[] jsonByteArray) ...
...
My question is:
- Is it possible to use different library for deserialization (ex. Json.Net) instead of system serialization methods?
- Or is there any other workaround for this?
Full call stack:
System.NotSupportedException: IL2CPP does not support marshaling delegates that point to instance methods to native code. The method we're attempting to marshal is: System.Runtime.Diagnostics.DiagnosticsEventProvider::EtwEnableCallBack
at System.Runtime.Interop.UnsafeNativeMethods.EventRegister (System.Guid& providerId, System.Runtime.Interop.UnsafeNativeMethods+EtwEnableCallback enableCallback, System.Void* callbackContext, System.Int64& registrationHandle) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Diagnostics.DiagnosticsEventProvider.EtwRegister () [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Diagnostics.DiagnosticsEventProvider..ctor (System.Guid providerGuid) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Diagnostics.EtwProvider..ctor (System.Guid id) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Diagnostics.EtwDiagnosticTrace.CreateEtwProvider (System.Guid etwProviderId) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Diagnostics.EtwDiagnosticTrace..ctor (System.String traceSourceName, System.Guid etwProviderId) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Fx.InitializeTracing () [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Fx.get_Trace () [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.FxTrace.get_Trace () [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Diagnostics.Application.TD.IsEtwEventEnabled (System.Int32 eventIndex) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Diagnostics.Application.TD.ImportKnownTypesStartIsEnabled () [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.DataContract.ImportKnownTypeAttributes (System.Type type, System.Collections.Generic.Dictionary2[TKey,TValue] typesChecked, System.Collections.Generic.Dictionary2[System.Xml.XmlQualifiedName,System.Runtime.Serialization.DataContract]& knownDataContracts) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.DataContract.ImportKnownTypeAttributes (System.Type type) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.ClassDataContract+ClassDataContractCriticalHelper.get_KnownDataContracts () [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.ClassDataContract.get_KnownDataContracts () [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize (System.Runtime.Serialization.XmlReaderDelegator reader, System.String name, System.String ns, System.Type declaredType, System.Runtime.Serialization.DataContract& dataContract) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize (System.Runtime.Serialization.XmlReaderDelegator xmlReader, System.Type declaredType, System.Runtime.Serialization.DataContract dataContract, System.String name, System.String ns) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize (System.Runtime.Serialization.XmlReaderDelegator xmlReader, System.Type declaredType, System.Runtime.Serialization.DataContract dataContract, System.String name, System.String ns) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalReadObject (System.Runtime.Serialization.XmlReaderDelegator xmlReader, System.Boolean verifyObjectName) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.XmlObjectSerializer.InternalReadObject (System.Runtime.Serialization.XmlReaderDelegator reader, System.Boolean verifyObjectName, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions (System.Runtime.Serialization.XmlReaderDelegator reader, System.Boolean verifyObjectName, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions (System.Runtime.Serialization.XmlReaderDelegator reader, System.Boolean verifyObjectName) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject (System.Xml.XmlDictionaryReader reader) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject (System.IO.Stream stream) [0x00000] in <00000000000000000000000000000000>:0
at Microsoft.Identity.Client.Utils.JsonHelper.DeserializeFromJson[T] (System.Byte[] jsonByteArray) [0x00000] in <00000000000000000000000000000000>:0
at Microsoft.Identity.Client.Utils.JsonHelper.DeserializeFromJson[T] (System.String json) [0x00000] in <00000000000000000000000000000000>:0
at Microsoft.Identity.Client.OAuth2.OAuth2Client.CreateResponse[T] (Microsoft.Identity.Client.Http.HttpResponse response, Microsoft.Identity.Client.Core.RequestContext requestContext, System.Boolean addCorrelationId) [0x00000] in <00000000000000000000000000000000>:0
at Microsoft.Identity.Client.OAuth2.OAuth2Client+<ExecuteRequestAsync>d__101[T].MoveNext () [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext (System.Object stateMachine) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ContextCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0
at System.Action.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction (System.Action action, System.Boolean allowInlining, System.Threading.Tasks.Task& currentTask) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task.FinishContinuations () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task.FinishStageThree () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task1[TResult].TrySetResult (TResult result) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1[TResult].Create () [0x00000] in <00000000000000000000000000000000>:0
at Microsoft.Identity.Client.Http.HttpManager+<SendGetAsync>d__5.MoveNext () [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext (System.Object stateMachine) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ContextCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0
at System.Action.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction (System.Action action, System.Boolean allowInlining, System.Threading.Tasks.Task& currentTask) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task.FinishContinuations () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task.FinishStageThree () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task1[TResult].TrySetResult (TResult result) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1[TResult].Create () [0x00000] in <00000000000000000000000000000000>:0
at Microsoft.Identity.Client.Http.HttpManager+<ExecuteWithRetryAsync>d__8.MoveNext () [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext (System.Object stateMachine) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ContextCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0
at System.Action.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction (System.Action action, System.Boolean allowInlining, System.Threading.Tasks.Task& currentTask) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task.FinishContinuations () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task.FinishStageThree () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task1[TResult].TrySetResult (TResult result) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1[TResult].Create () [0x00000] in <00000000000000000000000000000000>:0
at Microsoft.Identity.Client.Http.HttpManager+<ExecuteAsync>d__9.MoveNext () [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext (System.Object stateMachine) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ContextCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0
at System.Action.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction (System.Action action, System.Boolean allowInlining, System.Threading.Tasks.Task& currentTask) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task.FinishContinuations () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task.FinishStageThree () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task1[TResult].TrySetResult (TResult result) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1[TResult].Create () [0x00000] in <00000000000000000000000000000000>:0
at <PrivateImplementationDetails>.ComputeStringHash (System.String s) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext (System.Object stateMachine) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ContextCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0
at System.Action.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction (System.Action action, System.Boolean allowInlining, System.Threading.Tasks.Task& currentTask) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task.FinishContinuations () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task.FinishStageThree () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task1[TResult].TrySetResult (TResult result) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1[TResult].Create () [0x00000] in <00000000000000000000000000000000>:0
at <PrivateImplementationDetails>.ComputeStringHash (System.String s) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext (System.Object stateMachine) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ContextCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0
at System.Action.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction (System.Action action, System.Boolean allowInlining, System.Threading.Tasks.Task& currentTask) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task.FinishContinuations () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task.FinishStageThree () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.Task1[TResult].TrySetResult (TResult result) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.UnwrapPromise1[TResult].TrySetFromTask (System.Threading.Tasks.Task task, System.Boolean lookForOce) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.UnwrapPromise1[TResult].InvokeCore (System.Threading.Tasks.Task completingTask) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.Tasks.UnwrapPromise1[TResult].Invoke (System.Threading.Tasks.Task completingTask) [0x00000] in <00000000000000000000000000000000>:0 occurred
Thank you.
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (7 by maintainers)
Top GitHub Comments
@marcinwalus - thank you for the detailed tutorial. I think we should be able to look at this in our next milestone, if @jmprieur agrees.
@bgavrilMS I can prepare some tutorial and/or demo project with details and I will do it (sorry for delay but I will do it on Thursday)