从职位描述属性中获取员工 ID .NET 8

问题描述 投票:0回答:1

我正在尝试从 Azure EntraId 获取员工 ID 字段。有关此处显示的问题的更多详细信息:

enter image description here

在此屏幕截图中,您可以看到我尝试使用 MS Graph 客户端获取的

employee id
字段,但每次尝试时我都得到 null:

enter image description here

这是我用于通过图形客户端获取数据的代码:

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

c# azure-active-directory .net-8.0 asp.net-core-8
1个回答
0
投票

最初注册单租户 Microsoft Entra ID 应用程序,添加并授予应用程序类型

User.Read.All
API 权限:

enter image description here

我创建了一个测试用户作为

testUser1
并配置了
employeeId
mail

在 Portal 中,确保属性中的

Employee ID
不应该为 null,并且应该存在,如下所示:

enter image description here

使用下面修改后的代码,要获取特定用户的

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); } }

回复:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.