MSAL - 即使在 "API权限 "中没有配置,应用程序也可以请求权限。

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

我使用MSAL库创建了一个小型的.NET Core 3.1控制台应用程序,该应用程序请求的范围是 api://55a047a1-a0d1-4b6b-9896-751a848e1e06/testscope2

自定义API暴露了两个作用域

  • api:/55a047a1-a0d1-4b6b-9896-751a848e1e06testscope1。
  • api:/55a047a1-a0d1-4b6b-9896-751a848e1e06testscope2。

enter image description here

我还配置了另一个名为 test-app 在 Azure Active Directory 中代表我的控制台应用程序。

我只配置了一个 API 权限 (api://55a047a1-a0d1-4b6b-9896-751a848e1e06/testscope1)的应用。我的理解是,有了这样的配置,客户端应用程序将只能请求范围的 test1 而它不允许 test-app 请求 scope2

以下是截图

enter image description here

这是我的代码。

//<PackageReference Include="Microsoft.Identity.Client" Version="4.13.0" />
namespace console_client
{
    class Program
    {
        static void Main(string[] args)
        {
            #region Azure AD parameters
            var clientId = "dddeefa5-d95c-4931-a53d-2382deee27c3";
            var tenant = "-- MY TENANT ID--";
            var instance = "https://login.microsoftonline.com/";
            #endregion

            var client = PublicClientApplicationBuilder
                .Create(clientId)
                .WithDefaultRedirectUri()
                .WithAuthority($"{instance}{tenant}")
                .Build();

            List<string> scopes = new List<string>();

            try
            {
                // I was under impression that this call will throw as exception as
                // this app is requesting 'testscope2' which is not included in API Permissions
                // while configuring test-app in Azure Active Directory (dddeefa5-d95c-4931-a53d-2382deee27c3 )
                // But I was able to retrieve token back with testscope2 in it.
                scopes.Add("api://55a047a1-a0d1-4b6b-9896-751a848e1e06/testscope2");
                var authenticationResult = client.AcquireTokenInteractive(scopes).ExecuteAsync().Result;
                Console.WriteLine($"Interactive Access token is : {authenticationResult.AccessToken}");
            }
            catch (Exception ex)
            { 
                System.Console.WriteLine($"******* {ex.Message}");
            }
        }
    }
}

问题:

我错过了什么吗?为什么即使应用程序没有配置权限,我也能获得访问令牌?

谢谢你

.net-core azure-active-directory identityserver3 msal
1个回答
2
投票

TL;DR这是一个功能。

使用v2端点MSAL,你可以请求在你的应用程序清单中没有定义的作用域。静态 但您的应用程序也可以请求您的应用程序所需的权限。动态 当然,用户管理员仍然需要同意,一个应用程序不会在没有同意的情况下获得一个权限。

你的应用似乎是一个单租户应用,所以这对你来说并没有什么区别.它主要是针对多租户的SaaS应用,可以在应用注册manifest中要求最低需要的权限,然后根据需要为选择功能申请更多的权限。

顺便说一下,如果你想使用应用注册中定义的权限,你可以申请一个特殊的范围。api://55a047a1-a0d1-4b6b-9896-751a848e1e06/.default (你的app ID URI或客户端id+".default").这将使AAD查看你的app注册来决定检查同意哪些权限。

© www.soinside.com 2019 - 2024. All rights reserved.