1
votes

Im using ASP.Net Core 2. I need to update a user in a Azure AD with Microsofts Graph API. The API documentation states that i should send the properties of the user in the body and specify the user in the URL like so:

https://graph.windows.net/myorganization/users/{user_id}?api-version

The documentation states that it should be a PATCH request. But HTTPRequestMessage does not accept PATCH as a HttpMethod. What is the proper way to make a PATCH request with asp.net core 2?

When i google i find that all answers suggests using JsonPatch, but that is a format that is not supported by Microsoft Graph API.

This is what i have so far....

        var client = new HttpClient();
        var requestUri = $"{_azureAdOptions.GraphInstance}/{_azureAdOptions.GraphVersion}/{_azureAdOptions.Domain}/users/me";

        var request = new HttpRequestMessage(HttpMethod., requestUri);

        var accessToken = await _authenticationHelper.GetAccessTokenAsync();
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
        var response = await client.SendAsync(request);
        var responseString = await response.Content.ReadAsStringAsync();
1

1 Answers

0
votes

Given the nature of Http, and the craziness of developers, you can create your own http server with custom methods. The class HttpMethod was created with this in mind allowing you to specify the method as string:

var method = new HttpMethod("PATCH"); // Patch
var request = new HttpRequestMessage(method , requestUri); // Use patch

Note: The new version of HttpClient comes with Patch method by default.