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'已过时:'Use GetAccountsAsync 相反(请参见 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
和
参数2:无法从“ Microsoft.Identity.Client.IUser”转换为 '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);
感谢帮助 斯蒂芬
答案 0 :(得分:3)
MSALv1和v2之间进行了一些更改。这是blog post,详细说明了两者之间的区别。如错误消息中所述,具有字段IUser
的字段或属性的类型现在引用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。
该小组还在努力对公共api(MSAL v3)进行进一步的更改,可以对其进行审核here。