首页 > 解决方案 > AADSTS70011:为输入参数“范围”提供的值无效

问题描述

所以我有一个场景,其中应用程序应该在某些条件下将用户添加到组中。此外,当应用程序开始运行时,不应要求用户登录他们的 microsoft id/pwd。

因此,我访问了使用 Graph Service Client 对象创建的令牌,如下所示:

    GraphServiceClient graphClient = new GraphServiceClient(
        "https://graph.microsoft.com/v1.0", 
        new DelegateAuthenticationProvider(
            async (requestMessage) =>
            {
                string clientId = "My APP ID";
                string authorityFormat = "https://login.microsoftonline.com/{0}/v2.0";
                string tenantId = "tenant GUID";
                string[] _scopes = new string[] { 
                    "https://graph.microsoft.com/User.ReadBasic.All" 
                };
                // Custom Redirect URI asigned in the Application Registration 
                // Portal in the native Application Platform
                string redirectUri = "https://localhost:4803/"; 
                string clientSecret = "App Secret";
                ConfidentialClientApplication daemonClient = new ConfidentialClientApplication(
                    clientId, 
                    String.Format(authorityFormat, tenantId), 
                    redirectUri, 
                    new ClientCredential(clientSecret), 
                    null, new TokenCache()
                );
                AuthenticationResult authResult = await daemonClient.AcquireTokenForClientAsync(_scopes);
                string token = authResult.AccessToken;
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
            }
       )
    );

所以我尝试执行 var user = await graphClient.Me.Request().GetAsync();

我收到此错误:

AADSTS70011:为输入参数“范围”提供的值无效。范围 user.read 无效。

我也尝试使用 just User.ReadBasicas 范围,但得到相同的错误。

我在这里做错了什么?

标签: c#microsoft-graph-api

解决方案


您在此处使用客户端凭据流,这意味着您不能动态请求范围。您必须在 apps.dev.microsoft.com 中为您的应用注册配置所需的权限范围,然后将代码中的范围值设置为https://graph.microsoft.com/.default.

有关更多详细信息,请参阅https://developer.microsoft.com/en-us/graph/docs/concepts/auth_v2_service


推荐阅读