繁体   English   中英

.NET Core 2.2:如何在 Azure 应用服务中从 Azure AD 自动填充用户列表?

[英].NET Core 2.2 : How to auto-populate list of users from Azure AD in Azure app service.?

我的应用程序正在使用 open id connect 身份验证,使用的资源是图 API 来生成访问令牌。

.AddOpenIdConnect(options =>
            {
                options.ClientId = azureAdConfig.ClientId;
                options.ClientSecret = azureAdConfig.ClientSecret;
                options.Authority = string.Format(azureAdConfig.AADInstance, azureAdConfig.Tenant);
                options.ResponseType = OpenIdConnectResponseType.CodeIdToken;
                options.Resource = azureAdConfig.ResourceURI_Graph;
                options.Events = new AuthEvents(azureAdConfig, connectionStringsConfig);
            });

以下代码在本地运行良好,它使用DirectorySearcher class 使用 LDAP 协议从目录中查找用户。

 DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
        var defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value;      
  
   using (DirectorySearcher searcher = new DirectorySearcher("LDAP://" + defaultNamingContext))
                    {
           searcher.Filter = "(&(objectClass=user)(objectcategory=person)(displayName=" + username + "*))";
          SearchResult userProperty = searcher.FindOne();

}

但是,一旦部署到 azure 应用服务,相同的代码就会失败,并出现Access denied 异常。

    System.Runtime.InteropServices.COMException:
   at System.DirectoryServices.DirectoryEntry.Bind (System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
  

我有一个令牌,但我将如何重用使用 open Id 生成的令牌来创建一个 graphapi 客户端并获取用户的信息。

这是一个很好的例子,说明如何使用 oidc 和 msal 来获取令牌、保存并使用它来调用图: https://github.com/microsoftgraph/aspnetcore-connect-sample来调用图,最终检查/helpers/graphsdkhelper.cs 这适用于 .netcore 2.1,但 2.2 几乎相同。

如果你有令牌,你创建一个 GraphServiceClient 给它一个身份验证 header 与承载/令牌类似

requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

并使用图形客户端进行调用。

该示例还具有如何缓存令牌以供这样使用。

Microsoft 提供的另一个示例是这里的最新示例: https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/2-WebApp-graph-user/2-1 -Call-MSGraph

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM