Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Microsoft Identity Client 1更新到2,7后出现问题_C#_Asp.net_Msal - Fatal编程技术网

C# 从Microsoft Identity Client 1更新到2,7后出现问题

C# 从Microsoft Identity Client 1更新到2,7后出现问题,c#,asp.net,msal,C#,Asp.net,Msal,使用Microsoft Identity Client 1.0,sampel代码运行得非常完美。 更新到Microsoft Identity Client 2.7后,我出现了两个错误 ClientApplicationBase.Users已过时:“使用GetAccountsAsync” 相反(参见 )'WebApp OpenIDConnect DotNet C:\Users\IDE00053\Downloads\active-directory-b2c-dotnetcore-WebApp-mas

使用Microsoft Identity Client 1.0,sampel代码运行得非常完美。 更新到Microsoft Identity Client 2.7后,我出现了两个错误

ClientApplicationBase.Users已过时:“使用GetAccountsAsync” 相反(参见 )'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

在代码行

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);
谢谢你的帮助
Stefan

MSALv1和v2之间发生了一些变化。下面是详细说明两者之间差异的示例。如错误消息中所述,具有
IUser
类型的字段或属性的类型现在引用。和
ClientApplicationBase.Users
变成
GetAccountsAsync()
。 根据您的代码,您需要以下内容:

AuthenticationResult result = await cca.AcquireTokenSilentAsync(scope, cca.Users.FirstOrDefault(), AzureAdB2COptions.Authority, false);
IEnumerable accounts=wait app.GetAccountsAsync();
IAccount firstAccount=accounts.FirstOrDefault();
尝试
{
结果=等待app.AcquireTokenSilentAsync(作用域,第一个帐户);
}
捕获(MsalUiRequiredException)
{
结果=wait app.AcquireTokenAsync(作用域,firstAccount);
}
accounts=wait app.GetAccountsAsync();
firstAccount=accounts.FirstOrDefault();
IAccount me=wait app.GetAccountAsync(firstAccount.HomeAccountId.Identifier);
以下是已更新为MSAL v2的


该团队还致力于对公共api(MSAL v3)进行进一步修改,可以对其进行审查

谢谢你的回答。这很有帮助。但是我仍然对asp.net核心应用程序的体系结构有疑问。在Azure AD B2C上使用MSAL进行身份验证更好还是不在本网站上使用MSALStefan@Stefan您可以使用ASP.NET核心OpenID Connect中间件(如本文所述),也可以使用OpenID Connect混合流w/MSAL,如本文所示(尚未在msalv2上)。第二个示例显示了如何使用将身份验证代码兑换为访问令牌,该令牌保存在缓存中以供以后使用。
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);