Stefan
Stefan

Reputation: 585

Problem after update from Microsoft Identity Client 1 to 2,7

var scope = AzureAdB2COptions.ApiScopes.Split(' ');
    string signedInUserID = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
    TokenCache userTokenCache = new MSALSessionCache(signedInUserID, this.HttpContext).GetMsalCacheInstance();
    ConfidentialClientApplication cca = new ConfidentialClientApplication(AzureAdB2COptions.ClientId, AzureAdB2COptions.Authority, AzureAdB2COptions.RedirectUri, new ClientCredential(AzureAdB2COptions.ClientSecret), userTokenCache, null);

    AuthenticationResult result = await cca.AcquireTokenSilentAsync(scope, cca.Users.FirstOrDefault(), AzureAdB2COptions.Authority, false);

    HttpClient client = new HttpClient();
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, AzureAdB2COptions.ApiUrl);

    // Add token to the Authorization header and make the request
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
    HttpResponseMessage response = await client.SendAsync(request);

with Microsoft Identity Client 1.0 the sampel code runs perfect. After update to Microsoft Identity Client 2.7 I become two errors

ClientApplicationBase.Users' is obsolete: 'Use GetAccountsAsync instead (See https://aka.ms/msal-net-2-released)' WebApp-OpenIDConnect-DotNet C:\Users\IDE00053\Downloads\active-directory-b2c-dotnetcore-webapp-master\WebApp-OpenIDConnect-DotNet\Controllers\HomeController.cs

and

Argument 2: cannot convert from 'Microsoft.Identity.Client.IUser' to 'Microsoft.Identity.Client.IAccount' WebApp-OpenIDConnect-DotNet C:\Users\IDE00053\Downloads\active-directory-b2c-dotnetcore-webapp-master\WebApp-OpenIDConnect-DotNet\Controllers\HomeController.cs

at the code line

AuthenticationResult result = await cca.AcquireTokenSilentAsync(scope, cca.Users.FirstOrDefault(), AzureAdB2COptions.Authority, false);

Thanks for help Stefan

Upvotes: 3

Views: 4243

Answers (1)

Jenny
Jenny

Reputation: 1229

There were several changes made between MSALv1 and v2. Here is the blog post which details the differences between the two. As noted in the error messages, the types that had fields or properties of type IUser now reference IAccount. And ClientApplicationBase.Users becomes GetAccountsAsync(). Depending on your code, you'd need something like this:

IEnumerable<IAccount> accounts = await app.GetAccountsAsync();
IAccount firstAccount = accounts.FirstOrDefault();
try
{
    result = await app.AcquireTokenSilentAsync(scopes, firstAccount);
}
catch (MsalUiRequiredException)
{
    result = await app.AcquireTokenAsync(scopes, firstAccount);
}
accounts = await app.GetAccountsAsync();
firstAccount = accounts.FirstOrDefault();
IAccount me = await app.GetAccountAsync(firstAccount.HomeAccountId.Identifier);

Here is a B2C sample which has been updated to MSAL v2.

The team is also working on further changes to the public api (MSAL v3), which can be reviewed here.

Upvotes: 5

Related Questions