如何刷新 Azure B2C IDP 传递令牌

问题描述 投票:0回答:0

使用带有 IDP 直通的 Microsoft Azure B2C (https://learn.microsoft.com/en-us/azure/active-directory-b2c/idp-pass-through-user-flow?pivots=b2c-user-flow ) 结合 Blazor WASM 应用程序,我遇到了一个问题,即 Azure B2C jwt 承载在到期后正确刷新,但是内部 idp 声明已过期并且 Azure B2C 没有更新。我正在使用库存 MSAL。

“内部”idp 已由 Microsoft Azure AD 提供,使用此处详述的多 AD 自定义策略:(https://learn.microsoft.com/en-us/azure/active-directory-b2c/identity-provider -azure-ad-multi-tenant?pivots=b2c-custom-policy)

我的应用程序在几个地方与 Graph API 集成,以从登录用户 Microsoft 的 365 邮件帐户发送电子邮件。这在“内部”令牌到期之前效果很好。

即使在这个初始到期之后,我的网站通常也能正常工作,因为 MSAL 正确处理了“外部”令牌刷新。

但是,一旦我调用我的应用程序中使用 Idp 令牌从用户发送电子邮件的端点,它就会失败。 (并使站点崩溃——我认为这是因为内部 Graph API 抛出一个 ServiceException——但我没有花更多时间在这上面——然而 Graph Api 是唯一导致站点崩溃的异常,我可以看到容器Azure 门户中记录的崩溃)

下面是代码示例:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Graph;
using System.Linq;
using System.Threading.Tasks;


namespace MyApp.Controllers
{


    [ApiController]
 
    public class SampleController : Controller
    {

        [HttpGet("/api/testsendmail")]

        public async Task<ActionResult> Send()
        {

            var user = HttpContext.User;
            var idpToken = user.Claims.Where(c => c.Type == "idp_access_token")
                   .Select(c => c.Value).SingleOrDefault();

            
            if (idpToken != null)
            {

                var client = OnBehalfOfGraphClient.GetGraphClient(idpToken);


                var mail = client.Me.SendMail(new Message()
                {
                    ToRecipients = new Recipient[] {
                        new Recipient() {
                            EmailAddress = new EmailAddress() { Address = "[email protected]" }
                        } },
                    Subject = "Test",
                    Body = new Microsoft.Graph.ItemBody() { ContentType = BodyType.Text, Content = "Test" },

                }, true).Request();

               // If the token specified in idpToken is invalid the following throws an IDP token exception, then crashes the site.
               // prior to the token expiring out this works as expected.
                var resp = await mail.PostResponseAsync();
            }
            else
            {
                
            }    

            return Ok();
        }




        public static class OnBehalfOfGraphClient
        {
            public static GraphServiceClient GetGraphClient(string token)
            {

                var authProvider = new DelegateAuthenticationProvider(async (request) =>
                {
                    request.Headers.Authorization =
                        new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
                });

                var graphClient = new GraphServiceClient(authProvider);

                return graphClient;

            }
        }


    }
}

在短期内,我已经“保护”了我的应用程序中可能遇到此问题的端点,方法是手动检查 idp 令牌的到期时间并发送带有 json 问题详细信息正文的 403 响应。至少这允许我提示用户注销并重新登录,但这显然不是最佳选择。 (理想情况下,我希望 MSAL 处理正在更新的内部令牌或提示用户重新进行身份验证)。

感谢收到的任何帮助。

.net-core oauth-2.0 blazor-webassembly msal azure-b2c
© www.soinside.com 2019 - 2024. All rights reserved.