Thank you for the detailed solution.
Unfortunately, it still does not work. Let me provide more details.
if (provider.State == ProviderState.SignedOut)
{
bool bSilentOk = await provider.TrySilentSignInAsync();
if (bSilentOk)
{
//Great, do nothing and continue to use the provider
}
else
{
await provider.SignInAsync();
}
}
await provider.SignInAsync() shows the following dialog:👁 User's image
When an account is selected, the following exception
System.NullReferenceException: 'Object reference not set to an instance of an object.'
is thrown at
foreach (KeyValuePair<string, string> mSATokenRequestProperty in _webAccountProviderConfig.MSATokenRequestProperties)
in
`System.NullReferenceException: 'Object reference not set to an instance of an object.'`
```is thrown at
`foreach (KeyValuePair<string, string> mSATokenRequestProperty in _webAccountProviderConfig.MSATokenRequestProperties)`
in
```go
private WebTokenRequest GetWebTokenRequest(WebAccountProvider provider, string clientId, string[] scopes)
{
//IL_001d: Unknown result type (might be due to invalid IL or missing references)
//IL_0014: Unknown result type (might be due to invalid IL or missing references)
//IL_0023: Expected O, but got Unknown
string text = string.Join(' ', scopes);
WebTokenRequest val = (string.IsNullOrWhiteSpace(clientId) ? new WebTokenRequest(provider, text) : new WebTokenRequest(provider, text, clientId));
val.Properties.Add("resource", "https://graph.microsoft.com");
if (provider.Authority == "consumers")
{
foreach (KeyValuePair<string, string> mSATokenRequestProperty in _webAccountProviderConfig.MSATokenRequestProperties)
{
val.Properties.Add(mSATokenRequestProperty);
}
}
else if (provider.Authority == "organizations")
{
foreach (KeyValuePair<string, string> aADTokenRequestProperty in _webAccountProviderConfig.AADTokenRequestProperties)
{
val.Properties.Add(aADTokenRequestProperty);
}
}
return val;
}
which is a method of Public class WindowsProvider : BaseProvider.
VS shows authResult = await AuthenticateInteractiveAsync(scopes); throws the exception with the following stack:
This exception was originally thrown at this call stack:
CommunityToolkit.Authentication.WindowsProvider.GetWebTokenRequest(Windows.Security.Credentials.WebAccountProvider, string, string[]) in WindowsProvider.cs
CommunityToolkit.Authentication.WindowsProvider.AuthenticateInteractiveAsync.AnonymousMethod__0() in WindowsProvider.cs
from:
public override async Task<string> GetTokenAsync(bool silentOnly = false)
{
await SemaphoreSlim.WaitAsync();
try
{
string[] scopes = _scopes;
WebTokenRequestResult authResult = await AuthenticateSilentAsync(scopes);
WebTokenRequestResult obj = authResult;
if (obj == null || (int)obj.ResponseStatus > 0)
{
if (silentOnly)
{
return null;
}
authResult = await AuthenticateInteractiveAsync(scopes);
}
WebTokenRequestResult obj2 = authResult;
if (obj2 != null && (int)obj2.ResponseStatus == 0)
{
WebAccount webAccount = authResult.ResponseData[0].WebAccount;
await SetAccountAsync(webAccount);
return authResult.ResponseData[0].Token;
}
WebTokenRequestResult obj3 = authResult;
if (obj3 != null && (int)obj3.ResponseStatus == 1)
{
return null;
}
WebTokenRequestResult obj4 = authResult;
if (((obj4 != null) ? obj4.ResponseError : null) != null)
{
throw new Exception(authResult.ResponseError.ErrorCode + ": " + authResult.ResponseError.ErrorMessage);
}
throw new Exception("Token request was not successful, but is also missing an error message.");
}
catch (Exception ex)
{
throw ex;
}
finally
{
SemaphoreSlim.Release();
}
}
private Task<WebTokenRequestResult> AuthenticateInteractiveAsync(string[] scopes)
{
//IL_002c: Unknown result type (might be due to invalid IL or missing references)
//IL_0036: Expected O, but got Unknown
TaskCompletionSource<WebTokenRequestResult> tcs = new TaskCompletionSource<WebTokenRequestResult>();
if (!DispatcherQueue.TryEnqueue((DispatcherQueueHandler)async delegate
{
_ = 2;
try
{
WebAccount webAccount = _webAccount;
WebTokenRequestResult result;
if (webAccount != null)
{
WebAccountProvider webAccountProvider = webAccount.WebAccountProvider;
result = await WebAuthenticationCoreManager.RequestTokenAsync(GetWebTokenRequest(webAccountProvider, _webAccountProviderConfig.ClientId, scopes), webAccount);
}
else
{
WebAccountProvider provider = await ShowAccountSettingsPaneAndGetProviderAsync();
result = await WebAuthenticationCoreManager.RequestTokenAsync(GetWebTokenRequest(provider, _webAccountProviderConfig.ClientId, scopes));
}
tcs.SetResult(result);
}
catch (Exception exception)
{
tcs.SetException(exception);
}
}))
{
tcs.SetException(new InvalidOperationException("Failed to enqueue the operation."));
}
return tcs.Task;
}
The sequence:
GetTokenAsync > AuthenticateInteractiveAsync > GetWebTokenRequest > _webAccountProviderConfig.MSATokenRequestProperties > NullReferenceException