.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 应用程序服务后失败,出现 拒绝访问异常。
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
我的应用程序正在使用 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 应用程序服务后失败,出现 拒绝访问异常。
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