从 Microsoft Identity Client 1 更新到 2,7 后出现问题
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);
使用 Microsoft Identity Client 1.0,样本代码运行完美。
更新到 Microsoft Identity Client 2.7 后出现两个错误
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
和
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
在代码行
AuthenticationResult result = await cca.AcquireTokenSilentAsync(scope, cca.Users.FirstOrDefault(), AzureAdB2COptions.Authority, false);
感谢帮助
斯特凡
MSALv1 和 v2 之间进行了多项更改。这里是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
。 ClientApplicationBase.Users
变为 GetAccountsAsync()
。
根据您的代码,您需要这样的东西:
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);
这里有一个 B2C sample 已经更新到 MSAL v2。
该团队还在努力对 public api (MSAL v3) 进行进一步更改,可以对其进行审核 here。
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);
使用 Microsoft Identity Client 1.0,样本代码运行完美。 更新到 Microsoft Identity Client 2.7 后出现两个错误
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
和
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
在代码行
AuthenticationResult result = await cca.AcquireTokenSilentAsync(scope, cca.Users.FirstOrDefault(), AzureAdB2COptions.Authority, false);
感谢帮助 斯特凡
MSALv1 和 v2 之间进行了多项更改。这里是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
。 ClientApplicationBase.Users
变为 GetAccountsAsync()
。
根据您的代码,您需要这样的东西:
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);
这里有一个 B2C sample 已经更新到 MSAL v2。
该团队还在努力对 public api (MSAL v3) 进行进一步更改,可以对其进行审核 here。