question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Unity Windows Standalone il2cpp: NotSupportedException: System.Reflection.Emit.DynamicMethod::.ctor

See original GitHub issue

Which Version of MSAL are you using ? MSAL 4.30

Platform net45 compiled with Unity Standalone il2cpp configuration

What authentication flow has the issue?

  • Desktop / Mobile
    • Interactive
    • Integrated Windows Auth
    • Username Password
    • Device code flow (browserless)
  • Web App
    • Authorization code
    • OBO
  • Daemon App
    • Service to Service calls

Other? - please describe;

Is this a new or existing app? c. This is a new app or experiment

Repro Create a Unity project and add MSAL authentication code similar to the below in a Unity component. Target Standalone and use the IL2CPP scripting backend.

private void BeforeAccessNotification(TokenCacheNotificationArgs args)
{
    lock (FileLock)
    {
        args.TokenCache.DeserializeMsalV3(File.Exists(CacheFilePath)
            ? ProtectedData.Unprotect(File.ReadAllBytes(CacheFilePath), null, DataProtectionScope.CurrentUser)
            : null);
    }
}

private void AfterAccessNotification(TokenCacheNotificationArgs args)
{
    // if the access operation resulted in a cache update
    if (args.HasStateChanged)
    {
        lock (FileLock)
        {
            // reflect changes in the persistent store
            File.WriteAllBytes(
                CacheFilePath,
                ProtectedData.Protect(
                    args.TokenCache.SerializeMsalV3(),
                    null,
                    DataProtectionScope.CurrentUser));
        }
    }
}

PublicClientApplicationBuilder builder = PublicClientApplicationBuilder.Create(clientId);
builder = builder.WithRedirectUri("http://localhost");
builder = builder.WithAuthority(AadAuthorityAudience.AzureAdAndPersonalMicrosoftAccount);
builder = builder.WithLogging(MsalLog, LogLevel.Error, true);
var PublicClientApp = builder.Build();
PublicClientApp.UserTokenCache.SetBeforeAccess(BeforeAccessNotification);
PublicClientApp.UserTokenCache.SetAfterAccess(AfterAccessNotification);

var authResult = await PublicClientApp.AcquireTokenSilent(scopes, Account).ExecuteAsync();

Expected behavior No dynamic code generation.

Actual behavior NotSupportedException: System.Reflection.Emit.DynamicMethod::.ctor

Possible Solution Building locally and removing the HAVE_CAS define seems to resolve the issue.

Additional context/ Logs / Screenshots Player.log

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
pmaytakcommented, May 3, 2021

@bgavrilMS

Method Mean (with flag, current) Mean (without flag)
Serialize_MsalTokenResponse_Test 4.256 μs 7.438 μs
Deserialize_MsalTokenResponse_Test 8.685 μs 13.194 μs
Serialize_InstanceDiscoveryResponse_Test 30.063 μs 42.920 μs
Deserialize_InstanceDiscoveryResponse_Test 47.900 μs 78.104 μs
Serialize_OAuth2ResponseBase_Test 1.574 μs 2.347 μs
Deserialize_OAuth2ResponseBase_Test 2.724 μs 4.348 μs
Serialize_Desrialize_Cache 146μs 160μs
0reactions
bgavrilMScommented, May 3, 2021

I also ran a test for serializing / deserializing the token cache (I added a row in the table above). All in all, it does not seem to be bad. I think we can:

  • implement this for both public client & confidential client (very simple change)
  • implement this only for public client (a bit more work)

@henrik-me - thoughts?

Read more comments on GitHub >

github_iconTop Results From Across the Web

NotSupportedException (With log detail)
The first error (the one as runtime) indicates that IL2CPP does not support System.Reflection.Emit, which is expected.
Read more >
Unity Json.net System.Reflection.Emit error in iOS
The error in this case is unrelated to managed code stripping or the link.xml file. This error occurs because the Newtonsoft JSON library...
Read more >
Manual: Scripting restrictions
IL2CPP does not support reflection of the MarhsalAs and FieldOffset attributes at runtime. It does support these attributes at compile time. You should...
Read more >
IL2CPP Overview
The IL2CPP backend converts MSIL (Microsoft Intermediate Language) code (for example, C# code in scripts) into C++ code, then uses the C++ code...
Read more >
System.Reflection.Emit.DynamicMethod::.ctor_abc1329的 ...
NotSupportedException : System.Reflection.Emit. ... IL2CPP 不支持运行时代码生成(引自unity3d - Unity Json.net System.Reflection.Emit error in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found