为什么 C# 应用程序无法通过 Microsoft graph API 将设备注册到 MS Intune?

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

我正在尝试创建一个 C# 应用程序,该应用程序将通过 Microsoft graph API 将设备注册到 Intune 中,但在调用 graphClient.DeviceManagement.WindowsAutopilotDeviceIdentities.PostAsync(newDevice) 后,它不断抛出此异常:

不存在与模板 ~/singleton/navigation 与请求 /DeviceEnrollmentFE/StatelessDeviceEnrollmentFEService/deviceManagement/windowsAutopilotDeviceIdentities 的 http 动词 POST 相匹配的 OData 路由。"} Microsoft.Graph.Models.ODataErrors.ODataError

首先,当我使用具有相同租户 ID/应用程序 ID/应用程序密钥(我在 C# 应用程序中使用的 powershell 脚本)时,可以将设备注册到 Intune 中。

该问题不应该是由于应用程序权限不足引起的。在 Azure AD 中,应用程序具有以下权限,所有这些权限都经过管理员同意:

  • DeviceManagementManagedDevices.ReadWrite.All
  • DeviceManagementServiceConfig.ReadWrite.All
  • DeviceManagementApps.ReadWrite.All

所有权限列表

当我调查与 MS 服务器的通信时,我的应用程序正确调用并接收来自服务器的访问令牌。但是调用register方法后没有数据包发送:

graphClient.DeviceManagement.WindowsAutopilotDeviceIdentities.PostAsync(newDevice);

并且它只是抛出上面提到的异常。

有人知道如何让 C# 应用程序通过 Graph API 与 MS Intune 通信吗?

这是我正在使用的代码的简化示例:

namespace IntuneTest
{
    public partial class MainWindow : Window
    {
        private GraphServiceClient graphClient;

        public MainWindow()
        {
            InitializeComponent();
            InitializeGraphClient();
        }

        // Initialize Graph Client with ClientSecretCredential for authentication
        private async void InitializeGraphClient()
        {
            var credential = new ClientSecretCredential(
                "TenantID",   // Replace with your Tenant ID
                "AppID",      // Replace with your App ID
                "ClientSecret" // Replace with your Client Secret
            );

            graphClient = new GraphServiceClient(credential, new[] { "https://graph.microsoft.com/.default" });

            var token = await credential.GetTokenAsync(new TokenRequestContext(new[] { "https://graph.microsoft.com/.default" }), CancellationToken.None);
            Debug.WriteLine($"Access Token: {token.Token}");
        }

        // Register a Windows Autopilot device
        private async Task RegisterDeviceAsync()
        {
            try
            {
                var newDevice = new WindowsAutopilotDeviceIdentity
                {
                    SerialNumber = "6WX8WT2",   // Required: Serial number
                    Manufacturer = "Dell Inc.", // Optional
                    Model = "Latitude 5590",    // Optional
                    EnrollmentState = EnrollmentState.Enrolled
                };

                await graphClient.DeviceManagement.WindowsAutopilotDeviceIdentities.PostAsync(newDevice);
                Debug.WriteLine("Device registered.");
                MessageBox.Show("Device registered successfully!", "Success");
            }
            catch (ServiceException ex)
            {
                Debug.WriteLine($"Error: {ex.Message}");
                MessageBox.Show($"Error registering device: {ex.Message}", "Error");
            }
        }

        // Triggered by button click to register the device
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            await RegisterDeviceAsync();
        }
    }
}

非常感谢!

c# microsoft-graph-api microsoft-graph-sdks intune
1个回答
0
投票

稍微澄清一下。

您使用的方法不应在 Intune 中注册设备。 该方法向 Windows Autopilot 服务注册设备。您确定这就是您想要实现的目标吗?

该方法可能会失败,因为您的调用缺少参数。 根据本文,所有 17 个属性都是必需的:Create windowsAutopilotDeviceIdentity 由于设备 ID 是必需属性的一部分,因此设备必须已加入 Entra ID 才能正常工作。

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