代码之家  ›  专栏  ›  技术社区  ›  AlexB

Microsoft Graph API测试-代表用户获取访问权限

  •  0
  • AlexB  · 技术社区  · 5 年前

    我有一段代码,它使用Graph API(.Net Core 3.1)为用户创建了一个扩展。我有一个针对这段代码的测试项目。但我需要以用户身份进行身份验证,才能创建和使用GraphServiceClient(用户具有全局管理员角色)。

    目标是有一个可工作的代码来创建 方案扩展 对于 用户 .

    现在,要创建扩展,客户端必须具有委托权限 Directory.AccessAsUser.All 该权限已授予门户中注册的应用程序。但由于这是一个委托权限,我需要以用户身份进行身份验证(在测试代码中)。因此,我的选择是针对身份验证提供者:

    • 授权码提供者
    • 代表供应商
    • 互动提供商

    对于授权码提供者:

                List<string> scopes = new List<string> { "Directory.AccessAsUser.All" };
    
                IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
                    .Create(_appClientId.ToString())
                    .WithRedirectUri(_redirectUri)
                    .WithClientSecret(_appSecret) // or .WithCertificate(certificate)
                    .Build();
    
                AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(confidentialClientApplication, scopes);
                _graphServiceClient = new GraphServiceClient(authProvider);
    

    我得到一个例外:

    微软。图表。认证。AuthenticationException:代码:身份验证质询必填

    代表提供商:

                IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
                    .Create(_appClientId.ToString())
                    .WithRedirectUri(_redirectUri)
                    .WithClientSecret(_appSecret)
                    .Build();
    
                OnBehalfOfProvider authProvider = new OnBehalfOfProvider(confidentialClientApplication, scopes);
    
                _graphServiceClient = new GraphServiceClient(authProvider);
    

    我明白了

    NullReferenceException

    当我试图实际创建模式时,在这一行:

    SchemaExtension extension = await _graphServiceClient .SchemaExtensions.Request().AddAsync(schemaExtension);
    

    对于互动提供商:

    IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
                .Create(clientId)
                .Build();
    
    InteractiveAuthenticationProvider authProvider = new InteractiveAuthenticationProvider(publicClientApplication, scopes);
    

    我得到:

    微软。身份。客户。MsalClientException:只支持环回重定向uri,但找到了urn:ietf:wg:oauth:2.0:oob。配置 http://localhost http://localhost:port 无论是在应用程序注册期间还是在创建PublicClientApplication对象时。

    最后一个我完全不明白。那么,我如何使此委托身份验证工作?

    补充

    以下是创建扩展但不依赖于授权的代码:

      SchemaExtension schemaExtension = new SchemaExtension
      {
        Id = schemaName.Trim(),
        // Owner = _appClientId.ToString(),    
        Description = string.IsNullOrWhiteSpace(schemaDesc) ? string.Empty : schemaDesc.Trim(),
        TargetTypes = new List<string>
        {
          "User"
        },
        Properties = new List<ExtensionSchemaProperty>
        {
          new ExtensionSchemaProperty
          {
            Name = "isGlobalAdmin",
            Type = "Boolean"
          },
          new ExtensionSchemaProperty
          {
            Name = "isOrganizationAdmin",
            Type = "Boolean"
          }
        }
      };
    
      SchemaExtension extension = await GraphClient.SchemaExtensions.Request().AddAsync(schemaExtension); // GraphClient here === _graphServiceClient in the code above
    
    0 回复  |  直到 5 年前
        1
  •  2
  •   Jim Xu    5 年前

    根据我的研究,Microsoft Graph的不同提供商使用不同的协议,适用于不同的环境。有关更多详细信息,请参阅 document

    对于授权码提供者:

    它使用 OAuth 2.0 authorization code flow 。通常,我们将其用于web应用程序访问web api的情况。有关更多详细信息,请参阅 doucment

    对于互动提供商

    它使用 OAuth 2.0授权代码流 。通常,我们将其用于桌面应用程序(如WPF)。此外,请注意,当我们将提供者与SST一起使用时。网, we must register "http://localhost" as a Public client (mobile & desktop) redirect URI for your AD application 。有关更多详细信息,请参阅 document


    更新

    如果我们想使用交互提供者调用Microsoft graph,请参考以下步骤

    1. 注册Azure AD应用程序 enter image description here
    2. 配置权限 enter image description here

    3. 代码

     static async Task Main(string[] args)
            {
    
                var clientId = "476944ed-e57c-4b2c-b18d-93b5dd5f1bca";
                string[] scopes = { "Directory.AccessAsUser.All" };
                #please provide the redirect url http://localhost when you create the client
                IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
                .Create(clientId)
                .WithRedirectUri("http://localhost") 
                .Build();
    
                InteractiveAuthenticationProvider authProvider = new InteractiveAuthenticationProvider(publicClientApplication, scopes);
    
                var graphClient = new GraphServiceClient(authProvider);
                var schemaExtension = new SchemaExtension
                {
                    Id = "courses",
                    Description = "Graph Learn training courses extensions",
                    TargetTypes = new List<string>()
                        {
                            "Group"
                        },
                    Properties = new List<ExtensionSchemaProperty>()
                        {
                            new ExtensionSchemaProperty
                            {
                                Name = "courseId",
                                Type = "Integer"
                            },
                            new ExtensionSchemaProperty
                            {
                                Name = "courseName",
                                Type = "String"
                            },
                            new ExtensionSchemaProperty
                            {
                                Name = "courseType",
                                Type = "String"
                            }
                        }
                };
    
                var result = await graphClient.SchemaExtensions.Request().AddAsync(schemaExtension);
                foreach (var type in result.TargetTypes) {
                    Console.WriteLine(type);
    
                }
    

    enter image description here