locked
Getting 400 Bad Request when trying to add a user to an AD group using Microsoft Graph RRS feed

  • Question

  • User1256154619 posted

    I am dealing with the extremely strange issue. The code that I built worked just fine one year ago, but now it doesn't.

    I am trying to add a user to an AD group but get 400 bad request error. What can I do to fix it? Should I switch to a certain version for ActiveDirectory package?

    Thank you in advance. Here is my function

    public static async Task<string> AddGroupMember(string accessToken, string groupId, string memberId)
    {
        var status = string.Empty;
        try
        {
            string endpoint = "https://graph.microsoft.com/v1.0/groups/" + groupId + "/members/$ref";
            string queryParameter = "";
    
            // pass body data 
            var keyOdataId = "@odata.id";
            var valueODataId = "https://graph.microsoft.com/v1.0/directoryObjects/" + memberId;
    
            var values = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>(keyOdataId, valueODataId)
            };
            var jsonData = $@"{{ ""{keyOdataId}"": ""{valueODataId}"" }}";
            var body = new StringContent(jsonData, Encoding.UTF8, "application/json");
    
    
            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, endpoint + queryParameter))
                {
                    request.Content = body;
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
    
                    using (var response = await client.SendAsync(request))
                    {
                        if (response.StatusCode == HttpStatusCode.NoContent)
                            status = "Member added to Group";
                        else
                            status = $"Unable to add Member to Group: {response.StatusCode}";
                    }
                }
            }
        }
        catch (Exception ex)
        {
            status = $"Error adding Member to Group: {ex.Message}";
        }
    
        return status;
    }

    Tuesday, October 29, 2019 4:55 PM

Answers

  • User1256154619 posted

    I found a solution. I tried to add users that already existed in a group. The issue of duplicate users caused the 400 Bad Request error

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 30, 2019 5:19 PM

All replies

  • User1724605321 posted

    Hi mpodolski,

    You can directly try the request in Microsoft Graph explorer :

    https://developer.microsoft.com/en-us/graph/graph-explorer

    If the request works , you can then check the request sending from c# client using browser's developer tool or fiddler , and find the difference .

    Best Regards,

    Nan Yu

    Wednesday, October 30, 2019 1:54 AM
  • User1256154619 posted

    I found a solution. I tried to add users that already existed in a group. The issue of duplicate users caused the 400 Bad Request error

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 30, 2019 5:19 PM