我正在尝试从 Azure EntraId 获取员工 ID 字段。有关此处显示的问题的更多详细信息:
在此屏幕截图中,您可以看到我尝试使用 MS Graph 客户端获取的
employee id
字段,但每次尝试时我都得到 null:
这是我用于通过图形客户端获取数据的代码:
private static GraphServiceClient GetGraphClient(string clientId, string tenantId, string clientSecret)
{
// Set up the client credential authentication provider
var confidentialClientApplication = ConfidentialClientApplicationBuilder.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority(new Uri($"https://login.microsoftonline.com/{tenantId}"))
.Build();
var authProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);
// Create and return the GraphServiceClient with the authentication provider
return new GraphServiceClient(authProvider);
}
这是获取员工的代码:
var result = await graphClient.Users
.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Filter = $"displayname eq '{User.Mail}'";
requestConfiguration.QueryParameters.Select = ["employeeId", "EmployeeID", "EMP_ID"];
});
我正在从索赔中获取所有其他基本详细信息。
请帮我也拿
employee id
。
最初注册单租户 Microsoft Entra ID 应用程序,添加并授予应用程序类型
User.Read.All
API 权限:
我创建了一个测试用户作为
testUser1
并配置了 employeeId
和 mail
。
在 Portal 中,确保属性中的
Employee ID
不应该为 null,并且应该存在,如下所示:
使用下面修改后的代码,要获取特定用户的
employee_ID
:
using Microsoft.Graph;
using Azure.Identity; // For ClientSecretCredential
using System;
using System.Threading.Tasks;
public class GraphClientExample
{
private static GraphServiceClient GetGraphClient(string clientId, string tenantId, string clientSecret)
{
// Use ClientSecretCredential for authentication
var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
// Return the GraphServiceClient using ClientSecretCredential
return new GraphServiceClient(clientSecretCredential);
}
public static async Task FetchEmployee(string clientId, string tenantId, string clientSecret, string userMail)
{
if (string.IsNullOrEmpty(userMail))
{
Console.WriteLine("User mail is null or empty.");
return;
}
// Initialize Graph client
var graphClient = GetGraphClient(clientId, tenantId, clientSecret);
try
{
// Fetch the user details
var result = await graphClient.Users
.GetAsync((requestConfiguration) =>
{
// Apply filter and select query
requestConfiguration.QueryParameters.Filter = $"mail eq '{userMail}'";
requestConfiguration.QueryParameters.Select = new[] { "id", "displayName", "mail", "employeeId" };
});
// Check if any users are found
if (result?.Value != null && result.Value.Count > 0)
{
foreach (var user in result.Value)
{
Console.WriteLine($"User Found: {user.DisplayName}, ID:{user.Id}, Email: {user.Mail}, Employee ID: { user.EmployeeId}");
}
}
else
{
Console.WriteLine("No matching user found.");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
public static async Task Main(string[] args)
{
// Replace these with your app registration details
string clientId = "<YOUR CLIENT ID";
string tenantId = "YOUR TENANAT ID";
string clientSecret = "YOUR CLIENT SECRET";
string userMail = "<YOUR TEST USER EMAIL SEARCH>"; // Email to search
await FetchEmployee(clientId, tenantId, clientSecret, userMail);
}
}
回复: