-
Notifications
You must be signed in to change notification settings - Fork 329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug] Crashing Microsoft.Identity.Client.Broker - WithBrokerPreview() #3654
Comments
Filed an internal bug against the partner |
I found new bugs,
vs.
and then, after clicking to an account, app crash silently without any debug message. |
Hi @pwallner with the repro steps you provided I see this error that leads to the crash you are seeing, 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.' Next MSAL release planned mid next week will have a fix for this issue. |
Hi @gladjohn This explains # 1 as I read, but not sure if it explains all other points. For now I use the broker (without preview), and there are no errors. |
Hi @pwallner for the original issue that you see,
I am able to repro this when no scopes is passed, this works with the old WAM experience but not the Preview. We will have a fix for this in the next MSAL release |
* [Bug] Crashing Microsoft.Identity.Client.Broker - WithBrokerPreview() #3654 * WamScopesRequired * mesaage updated * verbose * removing oidc scopes check Co-authored-by: Gladwin Johnson <gljohns@microsoft.com>
This will still fail, if user passes
this is tracked by the MSALRuntime team for a fix at their end that will also include checks on the oidc scopes. This will be released after MSAL 4.47.0 release. |
MSAL 4.47.0 has been released, can you please see if the crashes are fixed for you. |
hi @gladjohn the app isn't crashing so far (did only some sign in and outs), but I can see during logout (see code above):
if I catch the globalexceptions with
|
Hi @pwallner can you please provide the authority, scopes and the account type (AAD/MSA) that you are using? also, can you please open a separate issue for this? |
Hi @gladjohn I am using my common hotmail address for login. Opened new issue #3685 here the complete code as requested: using Microsoft.Graph;
using Microsoft.Identity.Client;
using Microsoft.Identity.Client.Broker;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using WinCopies.Linq;
namespace mcpat
{
public class GraphApi
{
private GraphServiceClient? graphClient;
readonly string tenantId = "common";
readonly string clientId = "xxxxxxxxxxxxxxxxxx";
readonly string[] scopes = new[] { "User.Read" };
private readonly IPublicClientApplication? _clientApp;
private IPublicClientApplication? PublicClientApp { get { return _clientApp; } }
public GraphApi()
{
try
{
_clientApp = PublicClientApplicationBuilder.Create(clientId)
.WithAuthority(AzureCloudInstance.AzurePublic, tenantId)
.WithDefaultRedirectUri()
.WithBrokerPreview()
.Build();
TokenCacheHelper.EnableSerialization(_clientApp.UserTokenCache);
}
catch (Exception ex)
{
Trace.WriteLine("Error GraphApi: " + ex.Message);
}
}
public async Task SignOut()
{
try
{
if (PublicClientApp == null)
return;
IEnumerable<IAccount> accounts = await PublicClientApp.GetAccountsAsync();
if (accounts.Any())
{
try
{
await PublicClientApp.RemoveAsync(accounts.FirstOrDefault());
Trace.WriteLine("User has signed-out");
}
catch (MsalException ex)
{
Trace.WriteLine($"MsalException Error signing-out user: {ex.Message}");
}
catch (Exception ex)
{
Trace.WriteLine($"Exception Error signing-out user: {ex.Message}");
}
}
} catch (Exception ex)
{
Trace.WriteLine($"Error signing-out user: {ex.Message}");
}
}
public async Task<AuthenticationResult?> GetTokenForUserAsync(Window window)
{
try
{
IAccount? firstAccount;
var app = PublicClientApp;
if (app == null)
return null;
var accounts = await app.GetAccountsAsync();
firstAccount = accounts.FirstOrDefault();
AuthenticationResult? authResult = null;
if (firstAccount != null)
authResult = await app.AcquireTokenSilent(scopes, firstAccount).ExecuteAsync();
else
authResult = await app.AcquireTokenInteractive(scopes)
.WithParentActivityOrWindow(new WindowInteropHelper(window).Handle)
.WithUseEmbeddedWebView(true)
.ExecuteAsync();
return authResult;
}
catch (MsalClientException ex)
{
int errorCode = Marshal.GetHRForException(ex) & ((1 << 16) - 1);
Trace.WriteLine("MsalClientException (ErrCode " + errorCode + "): " + ex.ErrorCode);
}
catch (Exception ex)
{
int errorCode = Marshal.GetHRForException(ex) & ((1 << 16) - 1);
Trace.WriteLine("Error Acquiring Token (ErrCode " + errorCode + "): " + ex);
}
return null;
}
public async Task LoginAsync(Window window)
{
try
{
if (_clientApp == null)
return;
AuthenticationResult? token = await GetTokenForUserAsync(window);
if (token == null)
return;
DelegateAuthenticationProvider authenticationProvider = new((request) =>
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);
return Task.FromResult(0);
});
graphClient = new GraphServiceClient(authenticationProvider);
} catch(Exception ex)
{
Trace.WriteLine("Error LoginAsync: " + ex.Message);
}
}
}
} |
Logs and network traces
During logout:
During login:
no logs, app close without any error in VisualStudio. But not always (~70% crash), sometimes login is working.
Which version of MSAL.NET are you using?
Microsoft.Identity.Client 4.46.2
Platform
.NET 6 WPF
What authentication flow has the issue?
Is this a new or existing app?
This is a new app or experiment.
Repro
Expected behavior
No crashing app, no errors on logout.
Actual behavior
During login, the app is crashing very often, during logout I get errors as above written
Possible solution
not using
WithBrokerPreview()
, instead see alternative code.Alternative working code
same as above, just remove
and you receive the "old" Loginscreen from Microsoft.
The text was updated successfully, but these errors were encountered: