Azure AD AcquireTokenOnBehalfOf

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

我需要能够使用我在客户端 Angular 应用程序中收到的令牌代表我的 API 中的用户获取令牌。我已经使用角度的 MSAL 包从我在 Azure 中的客户端应用程序注册中获取令牌。我已将其保存到我的数据库中,因此当我的 azure 函数运行时,它可以读取此令牌以从 Azure 中的 API 应用程序注册中获取新令牌。然后我需要读取用户的邮箱。

到目前为止,我可以像这样以角度获得令牌:

this.msalService.acquireTokenPopup({
      scopes: [
        "api://*******************/access_as_user", // permission to access API App Registration
        "Mail.ReadWrite"
      ],
      prompt: "consent",
      authority: "https://login.microsoftonline.com/common",
    })
      .subscribe({
        next: (response: any) => {
          var graphTokenDto: MicrosoftGraphDto = {
            accessToken: response.accessToken,
            expires: new Date(response.expiresOn).toISOString(), // Time to timestamp
            scopes: response.scopes,
            status: MicrosoftGraphStatus.Connected,
            tokenType: response.tokenType,
            username: response.account.username,
            idToken: response.idToken
          }
          this.userService.connectMailAccount(graphTokenDto);

        },
        error: (error: any) => {
          console.log(error);
        }
      })

在 .NET Api 中获取令牌

_settings = settings;

            IConfidentialClientApplication app = ConfidentialClientApplicationBuilder
                .Create(_settings.ClientId)
                .WithClientSecret(_settings.ClientSecret)
                .WithTenantId(_settings.TenantId)
                .Build();

            UserAssertion userAssertion = new UserAssertion(microsoftGraphToken.AccessToken);

            var scopes = microsoftGraphToken.Scopes.AsEnumerable();

            AuthenticationResult result = await app.AcquireTokenOnBehalfOf(scopes, userAssertion)
                .ExecuteAsync(); // EXCEPTION THROWN HERE        

所以基本上它会通过我的 api 将令牌存储到数据库中。现在,我有一个 azure 函数需要从这个数据库中读取(它确实如此),如果所需的令牌已过期,则获取一个令牌,但我收到了不正确的观众错误。

我的问题是,我不能 100% 确定这是不是正确的做法。

清理:

Azure 应用注册

客户端应用程序注册(client_id 1) Api 应用注册 (client_id 2)

我的应用程序

Angular(配置client_id 1) .NET API(配置 client_id 2)

.net angular .net-core azure-active-directory msal
© www.soinside.com 2019 - 2024. All rights reserved.