microsoftgraph / msgraph-sdk-dotnet

Microsoft Graph Client Library for .NET!
https://graph.microsoft.com
Other
699 stars 247 forks source link

Register Autopilot Devices not possible #1306

Closed SirHenryIII closed 2 years ago

SirHenryIII commented 2 years ago

Describe the bug I'm using version 4.24.0 in a .NET 4.8 Project. When I try to register a device in Autopilot I get an error and I'm unable to get it up and running.

To Reproduce This is the basic function that I created to test the function

private async void AddAutopilotDeviceAsync(GraphServiceClient graphClient)
        {
            ImportedWindowsAutopilotDeviceIdentity deviceInformation = new ImportedWindowsAutopilotDeviceIdentity()
            {
                //ODataType = "#microsoft.graph.importedWindowsAutopilotDeviceIdentity",
                HardwareIdentifier = Encoding.ASCII.GetBytes("xxxxx"),
                SerialNumber = "xxxxx",
                //GroupTag = "",
                //ProductKey = "",
                //AssignedUserPrincipalName = "",
                State = new ImportedWindowsAutopilotDeviceIdentityState()
                {
                    //ODataType = "microsoft.graph.importedWindowsAutopilotDeviceIdentityState",
                    //DeviceImportStatus = ImportedWindowsAutopilotDeviceIdentityImportStatus.Pending,
                    //DeviceRegistrationId = "",
                    //DeviceErrorCode = 0,
                    //DeviceErrorName = ""
                }
            };
            var result = await graphClient.DeviceManagement.ImportedWindowsAutopilotDeviceIdentities
                .Request()
                .AddAsync(deviceInformation);
        }

Expected behavior I expect that the device is registered within autopilot but actually I just receiver an error. I can create a json on my own and submit it with the Graph Explorer without an issue.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context I tried a different set of properties within the deviceInformation, so all the commented properties were tested as well

andrueastman commented 2 years ago

Hey @SirHenryIII,

Thanks for raising this.

Any chance you could share the json you used in Graph Explorer to help identify where the inconsistency may be?

SirHenryIII commented 2 years ago

Sure, I picked this from the WindowsAutoPilotIntune PowerShell Module:

{
    "@odata.type": "#microsoft.graph.importedWindowsAutopilotDeviceIdentity",
    "groupTag": "$groupTag",
    "serialNumber": "$serialNumber",
    "productKey": "",
    "hardwareIdentifier": "$hardwareIdentifier",
    "assignedUserPrincipalName": "$assignedUser",
    "state": {
        "@odata.type": "microsoft.graph.importedWindowsAutopilotDeviceIdentityState",
        "deviceImportStatus": "pending",
        "deviceRegistrationId": "",
        "deviceErrorCode": 0,
        "deviceErrorName": ""
    }
}

But I reduced it to this and it was also working fine.

{
    "@odata.typ": "#microsoft.graph.importedWindowsAutopilotDeviceIdentity",
    "hardwareIdentifier": "xxxxx"
}

Thats the endpoint I used: https://graph.microsoft.com/v1.0/deviceManagement/importedWindowsAutopilotDeviceIdentities

andrueastman commented 2 years ago

Thanks for this @SirHenryIII,

Any chance the function works if you change from

HardwareIdentifier = Encoding.ASCII.GetBytes("xxxxx"),

to

HardwareIdentifier = Convert.FromBase64String("xxxxx"),

My understanding is that the first scenario may cause the corruption of the intended payload as Encoding.ASCII.GetBytes is ideally meant for text encoding and you would need to use the latter if you have base64encoded string.

SirHenryIII commented 2 years ago

Hey @andrueastman , that was an easy fix for my issue. It's working fine now. Did I missed any docs that describe the data type?

andrueastman commented 2 years ago

Hey @SirHenryIII,

The data type is a binary data type for the HardwareIdentifier outlined in the documentation at the link below.

https://docs.microsoft.com/en-us/graph/api/resources/intune-enrollment-importedwindowsautopilotdeviceidentity?view=graph-rest-1.0#properties

The issue was caused by incorrect encoding of the binary data as explained in the SO Answer.

Closing this for now. Feel free to create a new issue/re-open in the event of any other question/issue.