You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Error messages shown when running in Visual Studio:
First error:
Managed Debugging Assistant 'CallbackOnCollectedDelegate'
Message=Managed Debugging Assistant 'CallbackOnCollectedDelegate' : 'A callback was made on a garbage collected delegate of type 'Microsoft.Identity.Client.NativeInterop!Microsoft.Identity.Client.NativeInterop.API+MSALRUNTIME_COMPLETION_ROUTINE::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.'
Continue, then hit second error:
An unhandled exception of type 'System.NullReferenceException' occurred in Unknown Module.
Object reference not set to an instance of an object.
Entire program exits
Which version of MSAL.NET are you using?
We are using special preview versions of the Microsoft.Identity.Client packages created for compatibility with 4.5 with the help of @gladjohn. Specifically related to this problem, the listed version of the NativeInterop package is being copied to our bin folder after the build has completed.
Package References:
Platform
NET 4.5 with Asset Target Fallback to NET 4.6.1
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?
The app is in production but using an older version of sign-in that does not have this issue. We started noticing the issue when attempting to upgrade.
Repro
In our csproj (truncated to only the most important info):
internalasyncTask<SignInResult>SignInAsync(ClientTypeclientType){try{IPublicClientApplicationappAad=await CreateAuthClientAsync(clientType, isMSA:false);// ok to use just Aad client app since acquiring token interactive (with UI)AuthenticationResultresult=await appAad.AcquireTokenInteractive(newstring[]{ VsoResourceId }).WithPrompt(Prompt.SelectAccount).ExecuteAsync();// get data from VSOUserProfileResultprofile=await GetUserProfileAsync(result.AccessToken);if(profile==null){returnnew SignInResult(){State= SignInState.Error,Accounts=null,VsidAccountMap=null,Error="ERR_UNKNOWN_VSO_ACCOUNT",};}// return valueUserInfouser=new UserInfo
{email= result.Account.Username,name= profile.displayName,logo="data:image/png;base64,"+ profile.avatarBase64,providerId= profile.origin,stale=false,vsid= profile.id,};Dictionary<string,IAccount>accountMap=newDictionary<string,IAccount>{{ user.vsid, result.Account },};returnnew SignInResult
{State= SignInState.SignedIn,Accounts=new Accounts
{users=new UserInfo[1]{ user },},VsidAccountMap=accountMap,Error=null,};}catch(MsalClientExceptionmsalEx)when(msalEx.ErrorCode == MsalError.AuthenticationCanceledError){returnnew SignInResult(){State= SignInState.Cancelled,Accounts=null,VsidAccountMap=null,Error=null,};}catch(Exceptione){
Debug.WriteLine(e.ToString());returnnew SignInResult(){State= SignInState.Error,Accounts=null,VsidAccountMap=null,Error= e.ToString(),};}}privateasyncTask<IPublicClientApplication>CreateAuthClientAsync(ClientTypeclientType,boolisMSA){IPublicClientApplicationapp= GetPublicClientApplication(clientType, isMSA);stringcacheToUse=await GetCacheToUse(clientType);MsalCacheHelpercacheHelper=await CreateCacheHelper(cacheToUse);
cacheHelper.RegisterCache(app.UserTokenCache);returnapp;}private IPublicClientApplication GetPublicClientApplication(ClientTypeclientType,boolisMSA){stringclientId=(clientType== ClientType.VSPost2017_WAMDisabled_WAMClientId ||clientType== ClientType.VSPost2017_WAMEnabled)?VisualStudioClientId_WAMEnabled:VisualStudioClientId_NonWAM;PublicClientApplicationBuilderappBuilder= PublicClientApplicationBuilder.Create(clientId).WithLegacyCacheCompatibility(clientType== ClientType.VS2017)// uncomment and implement to support proxies outside of what .NET supports by default:// https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-net-provide-httpclient// part of VS uses just the .NET default code, part does not - we don't support authenticated proxies with User != SignedInUser// prior code we had in ServiceHub was also using .NET defaults, thus OK to keep as-is (commented)//.WithHttpClientFactory(IMsalHttpClientFactory...).WithDefaultRedirectUri().WithExtraQueryParameters(QueryParameters);// add tenant/authority informationappBuilder=isMSA?appBuilder= appBuilder.WithTenantId(ApplicationTenantId):appBuilder= appBuilder.WithAuthority(AadAuthorityAudience.AzureAdMultipleOrgs);// if using WAM, enable Windows brokerappBuilder=clientType== ClientType.VSPost2017_WAMEnabled ? EnableWAMIfAvailable(appBuilder):appBuilder;return appBuilder.Build();}private PublicClientApplicationBuilder EnableWAMIfAvailable(PublicClientApplicationBuilderAppBuilder){return
appBuilder
.WithExperimentalFeatures().WithWindowsBrokerOptions(new WindowsBrokerOptions
{MsaPassthrough=true,}).WithParentActivityOrWindow(()=> GetForegroundWindow()).WithBrokerPreview().WithLogging(MyLoggingMethod, LogLevel.Info, enablePiiLogging:true, enableDefaultPlatformLogging:false);}
Steps:
This happens sometimes but not every time I repeat the following steps:
Run the program
Select "Use another account"
When the account picker appears, either select an item from the list or enter new account information
Click continue
Expected behavior
User is successfully logged in with the selected account and redirected to our page.
Actual behavior
The program throws the following errors:
First error:
Managed Debugging Assistant 'CallbackOnCollectedDelegate'
Message=Managed Debugging Assistant 'CallbackOnCollectedDelegate' : 'A callback was made on a garbage collected delegate of type 'Microsoft.Identity.Client.NativeInterop!Microsoft.Identity.Client.NativeInterop.API+MSALRUNTIME_COMPLETION_ROUTINE::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.'
Continue, then hit second error:
An unhandled exception of type 'System.NullReferenceException' occurred in Unknown Module.
Object reference not set to an instance of an object.
Possible solution
It appears that garbage collection is being done on objects from the NativeInterop package before the necessary callback is successfully made. Garbage collection should not occur until the callback is made successfully.
Additional context / logs / screenshots / links to code
This issue is specifically occurring for us due to a couple of things:
We need to be compatible with NET 4.5, so we've had to work with @gladjohn to get packages specifically made for us which support this version
In the project that targets NET 4.5, we add an AssetTargetFallback to NET 4.6.1 to allow the code to run successfully when NET 4.6.1 is present on the machine
This is saying that there is a managed delegate that is being given to C wrapper. C wrapper invokes it, but the managed GC will have already collected it.... Now, we just have to find it :)
Logs and network traces
Logs: https://microsoft-my.sharepoint.com/:t:/p/laco/EeiLU2TnhIZPodyDkS5jA_oBqCjiIu-aMll07KumUjnwrQ?e=8w5w0o
Error messages shown when running in Visual Studio:
First error:
Continue, then hit second error:
Entire program exits
Which version of MSAL.NET are you using?
We are using special preview versions of the Microsoft.Identity.Client packages created for compatibility with 4.5 with the help of @gladjohn. Specifically related to this problem, the listed version of the NativeInterop package is being copied to our bin folder after the build has completed.
Package References:
Platform
NET 4.5 with Asset Target Fallback to NET 4.6.1
What authentication flow has the issue?
Is this a new or existing app?
The app is in production but using an older version of sign-in that does not have this issue. We started noticing the issue when attempting to upgrade.
Repro
In our csproj (truncated to only the most important info):
C# Code:
Steps:
This happens sometimes but not every time I repeat the following steps:
Expected behavior
User is successfully logged in with the selected account and redirected to our page.
Actual behavior
The program throws the following errors:
First error:
Continue, then hit second error:
Possible solution
It appears that garbage collection is being done on objects from the NativeInterop package before the necessary callback is successfully made. Garbage collection should not occur until the callback is made successfully.
Additional context / logs / screenshots / links to code
This issue is specifically occurring for us due to a couple of things:
For reference, our code can be found here for internal Microsoft users: https://devdiv.visualstudio.com/DefaultCollection/DevDiv/_git/Setup-Engine?path=/&version=GBdev/laco/WAM_elevatedProcess_fix
Relevant files:
The text was updated successfully, but these errors were encountered: