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] Interactive flow not working in Unity 2021

See original GitHub issue

Logs and network traces None

Which version of MSAL.NET are you using? 4.34 - the Microsoft.Identity.Client.dll is added as a Unity plugin (for Editor and Standalone only)

Platform .NET 4.5, Unity 2021

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 or experiment.

Repro

            string[] scopes = new string[] { "user.read" };

            var app = PublicClientApplicationBuilder
                         .Create(Preferences.ClientId)
                         .WithTenantId(Preferences.TenantId)
                         .WithLogging(MyLoggingMethod, LogLevel.Info,
                           enablePiiLogging: true,
                           enableDefaultPlatformLogging: true)
                         .Build();

            var accounts = await app.GetAccountsAsync();

            AuthenticationResult result = null;
            if (accounts.Any())
            {
                result = await app.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
                                    .ExecuteAsync();
            }
            else
            {
                try
                {
                    result = await app.AcquireTokenInteractive(scopes)
                                        .ExecuteAsync(CancellationToken.None);
                }
                catch (MsalUiRequiredException ex)
                { }
            }

Expected behavior The login popup appears or account is detected.

Actual behavior Errors appear in the log - nothing happens.

NotImplementedException: The method or operation is not implemented. System.Runtime.InteropServices.Marshal.ReadInt16 (System.Object ptr, System.Int32 ofs) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Drawing.Font.FromLogFont (System.Object lf, System.IntPtr hdc) (at <1e9166bfca654377b09cea42d003812d>:0) System.Drawing.Font.FromHfont (System.IntPtr hfont) (at <1e9166bfca654377b09cea42d003812d>:0) System.Drawing.SystemFonts.get_DefaultFont () (at <1e9166bfca654377b09cea42d003812d>:0) System.Windows.Forms.Control.get_DefaultFont () (at <3b28f5500d544e019e30dca09fab36d7>:0) System.Windows.Forms.Control.get_Font () (at <3b28f5500d544e019e30dca09fab36d7>:0) System.Windows.Forms.WebBrowserBase.get_Font () (at <3b28f5500d544e019e30dca09fab36d7>:0) System.Windows.Forms.Control.AssignParent (System.Windows.Forms.Control value) (at <3b28f5500d544e019e30dca09fab36d7>:0) System.Windows.Forms.Control+ControlCollection.Add (System.Windows.Forms.Control value) (at <3b28f5500d544e019e30dca09fab36d7>:0) Microsoft.Identity.Client.Platforms.Features.WinFormsLegacyWebUi.WindowsFormsWebAuthenticationDialogBase.<InitializeComponent>b__32_0 () (at <f4fac412be4346e4bdfeff1f81897b10>:0) Microsoft.Identity.Client.Platforms.Features.WinFormsLegacyWebUi.WindowsFormsWebAuthenticationDialogBase.InvokeHandlingOwnerWindow (System.Action action) (at <f4fac412be4346e4bdfeff1f81897b10>:0) Microsoft.Identity.Client.Platforms.Features.WinFormsLegacyWebUi.WindowsFormsWebAuthenticationDialogBase.InitializeComponent () (at <f4fac412be4346e4bdfeff1f81897b10>:0) Microsoft.Identity.Client.Platforms.Features.WinFormsLegacyWebUi.WindowsFormsWebAuthenticationDialogBase…ctor (System.Object ownerWindow) (at <f4fac412be4346e4bdfeff1f81897b10>:0) Microsoft.Identity.Client.Platforms.Features.WinFormsLegacyWebUi.WindowsFormsWebAuthenticationDialog…ctor (System.Object ownerWindow) (at <f4fac412be4346e4bdfeff1f81897b10>:0) (wrapper remoting-invoke-with-check) Microsoft.Identity.Client.Platforms.Features.WinFormsLegacyWebUi.WindowsFormsWebAuthenticationDialog…ctor(object) Microsoft.Identity.Client.Platforms.Features.WinFormsLegacyWebUi.InteractiveWebUI.OnAuthenticate () (at <f4fac412be4346e4bdfeff1f81897b10>:0) Microsoft.Identity.Client.Platforms.Features.WinFormsLegacyWebUi.WebUI.Authenticate (System.Uri requestUri, System.Uri callbackUri) (at <f4fac412be4346e4bdfeff1f81897b10>:0) Microsoft.Identity.Client.Platforms.Features.WinFormsLegacyWebUi.WebUI+<>c__DisplayClass20_0.<AcquireAuthorizationAsync>b__1 (System.Object tcs) (at <f4fac412be4346e4bdfeff1f81897b10>:0) — End of stack trace from previous location where exception was thrown — System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.ConfiguredTaskAwaitable1+ConfiguredTaskAwaiter[TResult].GetResult () (at <695d1cc93cca45069c528c15c9fdd749>:0) Microsoft.Identity.Client.Platforms.Features.WinFormsLegacyWebUi.WebUI+<AcquireAuthorizationAsync>d__20.MoveNext () (at <f4fac412be4346e4bdfeff1f81897b10>:0) --- End of stack trace from previous location where exception was thrown --- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.ConfiguredTaskAwaitable1+ConfiguredTaskAwaiter[TResult].GetResult () (at <695d1cc93cca45069c528c15c9fdd749>:0) Microsoft.Identity.Client.Internal.AuthCodeRequestComponent+<FetchAuthCodeAndPkceInternalAsync>d__7.MoveNext () (at <f4fac412be4346e4bdfeff1f81897b10>:0) — End of stack trace from previous location where exception was thrown — System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.ConfiguredTaskAwaitable1+ConfiguredTaskAwaiter[TResult].GetResult () (at <695d1cc93cca45069c528c15c9fdd749>:0) Microsoft.Identity.Client.Internal.AuthCodeRequestComponent+<FetchAuthCodeAndPkceVerifierAsync>d__4.MoveNext () (at <f4fac412be4346e4bdfeff1f81897b10>:0) --- End of stack trace from previous location where exception was thrown --- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.ConfiguredTaskAwaitable1+ConfiguredTaskAwaiter[TResult].GetResult () (at <695d1cc93cca45069c528c15c9fdd749>:0) Microsoft.Identity.Client.Internal.Requests.InteractiveRequest+<GetTokenResponseAsync>d__11.MoveNext () (at <f4fac412be4346e4bdfeff1f81897b10>:0) — End of stack trace from previous location where exception was thrown — System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.ConfiguredTaskAwaitable1+ConfiguredTaskAwaiter[TResult].GetResult () (at <695d1cc93cca45069c528c15c9fdd749>:0) Microsoft.Identity.Client.Internal.Requests.InteractiveRequest+<ExecuteAsync>d__8.MoveNext () (at <f4fac412be4346e4bdfeff1f81897b10>:0) --- End of stack trace from previous location where exception was thrown --- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.ConfiguredTaskAwaitable1+ConfiguredTaskAwaiter[TResult].GetResult () (at <695d1cc93cca45069c528c15c9fdd749>:0) Microsoft.Identity.Client.Internal.Requests.RequestBase+<RunAsync>d__12.MoveNext () (at <f4fac412be4346e4bdfeff1f81897b10>:0) — End of stack trace from previous location where exception was thrown — System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.ConfiguredTaskAwaitable1+ConfiguredTaskAwaiter[TResult].GetResult () (at <695d1cc93cca45069c528c15c9fdd749>:0) Microsoft.Identity.Client.ApiConfig.Executors.PublicClientExecutor+<ExecuteAsync>d__2.MoveNext () (at <f4fac412be4346e4bdfeff1f81897b10>:0) --- End of stack trace from previous location where exception was thrown --- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () (at <695d1cc93cca45069c528c15c9fdd749>:0) UCB.XR.Virtu.Services.Security.AuthenticationService+<GetIntegratedWindowsAuthenticationResultAsync>d__0.MoveNext () (at Assets/Scripts/UCB/XR/Services/Security/AuthenticationService.cs:41) — End of stack trace from previous location where exception was thrown — System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () (at <695d1cc93cca45069c528c15c9fdd749>:0) UCB.XR.Virtu.Security.AuthenticationManager+<Start>d__0.MoveNext () (at Assets/Scripts/XR/Security/AuthenticationManager.cs:11) — End of stack trace from previous location where exception was thrown — System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) (at <695d1cc93cca45069c528c15c9fdd749>:0) UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <adfa4b62400849189388df71c9e26e89>:0) UnityEngine.UnitySynchronizationContext:ExecuteTasks()

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
juanworkobotcommented, Jan 5, 2023

@bgavrilMS Thanks for the tip, worked great. Sharing my findings here, might help someone else. These works for a standalone windows build using Unity 2019.4.

  • Import Microsoft.Identity.Client and Microsoft.IdentityModel.Abstractions dlls to Assets/Plugins/x86_64. Kept getting errors without the Abstractions library in Unity 2019.4
  • Use WithTenantId if you want to restrict access only to the tenant.
  • WithRedirectUri and the Redirect URI in the app registration should be the same (“http://localhost”)
  • Use SystemWebViewOptions for custom redirect after flow complete.
  • Used ContinueWith task method. Other methods like async/await froze the unity window.
using Microsoft.Identity.Client;

void AthenticateWithInteractiveFlow()
{
        try
        {
            var app = PublicClientApplicationBuilder.Create("yourClientId").WithTenantId("yourClientTenant").WithRedirectUri("http://localhost").Build();
            string[] scopes = new string[] { "user.read" };
            
            var options = new SystemWebViewOptions()
            {
                HtmlMessageSuccess = "<p> Your custom success message here: {0}. Details {1}</p>",
                HtmlMessageError = "<p> Your custom error message here: {0}. Details {1}</p>",
                BrowserRedirectSuccess = new Uri("https://www.yourdomainhere.com"),
                //BrowserRedirectSuccess = new Uri("customURLProtocol://"), // How to create custom URL protocol in windows: https://stackoverflow.com/questions/80650/how-do-i-register-a-custom-url-protocol-in-windows
            };
            app.AcquireTokenInteractive(scopes).WithUseEmbeddedWebView(false).WithSystemWebViewOptions(options).ExecuteAsync().ContinueWith(myTask =>
            {
                AuthenticationResult result = myTask.Result;
                string accessToken = result.AccessToken;
                Debug.Log($"{accessToken}");
            });
        }
        catch (MsalException error)
        {
            Debug.Log(error.Message);
        }
}
1reaction
bgavrilMScommented, Jan 5, 2023

@juanworkobot - you can add a custom message or a custom redirect instead of the default Authentication complete. You can return to the application. Feel free to close this browser tab."

See https://learn.microsoft.com/en-us/dotnet/api/microsoft.identity.client.acquiretokeninteractiveparameterbuilder.withsystemwebviewoptions?view=azure-dotnet

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bug - Offline Support for Anonymous Authentication
Hello, I've just updated Remote Config to 3.1.3 (from 2.1.2) which ultimately requires that I start to use Unity Authentication.
Read more >
Is unity as big of a mess as people say it is lately? : r/ ...
As the saying goes: there are two ways to solve any problem in unity, but one is deprecated and the others experimental.
Read more >
Order of execution for event functions
Diagnose network issues · Creating custom packages · Naming your package · Package layout · Adding tests to a package · Creating samples...
Read more >
Manual: Coroutines
Solving network issues · Scoped registry authentication ... Upgrading to Unity 2021 LTS · Upgrading to Unity 2020 LTS ... Autodesk Interactive shader....
Read more >
Population Genetics and Microevolutionary Theory
... of selection and 479–483 unity of genotype and 476 Equilibrium: assortative ... two-loci 32–39 natural selection interaction with: gene flow interaction ......
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