Search code examples
c#azureazure-ad-msal

Unit Test project crashes on method AcquireTokenInteractive for instance IPublicClientApplication


I am migrating a unit test project from netcore3.0 to net6.0. When debugging in Visual Studio, I ran into a problem when calling the method AcquireTokenInteractive for instance of IPublicClientApplication.

This is the code snipped:

IPublicClientApplication PublicClientApp = PublicClientApplicationBuilder.Create(azureADAppSettings.ApplicationID)
            .WithTenantId(azureADAppSettings.TenantID)
            .WithAuthority(AzureCloudInstance.AzurePublic, tenantName)
            .WithRedirectUri(redirectUri)
            .Build();

result = await PublicClientApp.AcquireTokenInteractive(scopes).ExecuteAsync();

The method starts executing, the login page opens in browser, I login and receive this message in browser:

Authentication complete. You can return to the application. Feel free to close this browser tab.

After the message is displayed in browser, Visual Studio does not return to the method AcquireTokenInteractive, Visual Studio stops debugging, there are no error messages in error list. In the output I found two exceptions.

The first one is: Exception thrown: 'Azure.RequestFailedException' in System.Private.CoreLib.dll

The second error is: Exception thrown: 'Microsoft.Identity.Client.MsalServiceException' in System.Private.CoreLib.dll

I have made some changes to my app registration in portal azure based on the second error message. I add the missing redirect uri to app registration and set the application as public client type.

The second error remained after the changes I made in the app registration on portal azure.

Does anybody know what following actions could I do to resolve the error messages and receive the token ?


Solution

  • The issue was that the redirectUri was not added as a native url instead it was added as web URL.