【问题标题】:Problem after update from Microsoft Identity Client 1 to 2,7从 Microsoft Identity Client 1 更新到 2,7 后出现问题
【发布时间】:2019-01-27 12:19:33
【问题描述】:
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' 已过时:'使用 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);

感谢您的帮助 斯蒂芬

【问题讨论】:

    标签: c# asp.net msal


    【解决方案1】:

    在 MSALv1 和 v2 之间进行了几处更改。这是blog post,详细说明了两者之间的差异。如错误消息中所述,具有IUser 类型的字段或属性的类型现在引用IAccountClientApplicationBase.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

    【讨论】:

    • 感谢您的回答。它有很大帮助。但我仍然对 asp.net 核心应用程序的架构有疑问。使用 MSAL 与 Azure AD B2C 进行身份验证是否更好,或者不要像在此站点上那样使用 MSAL codereform.com/blog/post/…问候 Stefan
    • @Stefan 您可以使用b2c sample 中使用的 ASP.NET Core OpenID Connect 中间件,也可以使用带有 MSAL 的 OpenId Connect 混合流,如sample 中所示(尚未在 msalv2 上)。第二个示例展示了如何使用MSAL 将身份验证码兑换为访问令牌,该令牌保存在缓存中以供以后使用。
    猜你喜欢
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多