首页 > 解决方案 > 我在 Azure 中执行了“公开 API”,但无法将该范围的令牌提供给客户端程序

问题描述

我正在尝试构建 WebAPI 并希望使用范围来限制其他客户端应用程序的权限。我在 Expose an API 刀片上创建了一个范围“BuildingAccess”,并将其他客户端应用程序添加到具有该范围的授权列表中。但是,当我使用客户端程序尝试获取具有该范围的令牌时,我得到“ AADSTS70011: The provided request must include a 'scope' input parameter. The provided value for the input parameter 'scope' is not valid.”错误

 IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create("removed")
            .WithTenantId("removed")
            .WithClientSecret(ClientSecret)
            .Build();

        List<string> scopes = new List<string>();
        scopes.Add(".default");
        scopes.Add("https://localhost:44371/BuildingAccess");

        AuthenticationResult result = null;
        try
        {
            result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Token acquired \n");
            Console.ResetColor();
        }
        catch (MsalServiceException ex)
        when (ex.Message.Contains("AADSTS70011"))
        {
            // Invalid scope. The scope has to be of the form "https://resourceurl/.default"
            // Mitigation: change the scope to be as expected
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Scope provided is not supported");
            Console.ResetColor();
        }

唯一可行的格式是当范围设置为https://localhost:44371/.default 时。下面我添加 BuildingAccess 范围的所有其他组合都失败,并针对我尝试过的不同格式出现以下错误。

如果那个有效https://localhost:44371/.default,那么我的服务器端有一个错误,因为它失败了

抛出异常:Microsoft.IdentityModel.Tokens.dll 中的“Microsoft.IdentityModel.Tokens.SecurityTokenInvalidAudienceException”

我在客户端收到未经授权的响应。

标签: azurewebapiscopesazure-authentication

解决方案


1.您应该输入正确的格式范围,确保使用以下格式:api://{Your-Application-ID}/your_scope_name。

2.然后您应该授予API权限并选择管理员同意。 在此处输入图像描述


推荐阅读