Whenever I try to add associate a user with an organization, it returns “User doesn’t exist”, even though it exists (in the database) and with get.
private async Task<bool> AddUserToOrganization(string organizationId, string userId)
{
string adminToken = await GetAdminTokenAsync();
if (string.IsNullOrEmpty(adminToken))
{
throw new Exception("Failed to fetch admin token.");
}
// Set the authorization header
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", adminToken);
var orgUrl = $"{_configuration["Keycloak:BaseUrl"]}/admin/realms/{_configuration["Keycloak:Realm"]}/organizations/{organizationId}/members";
var requestBody = new { userId = userId }; // Create the HTTP request
var request = new HttpRequestMessage(HttpMethod.Post, orgUrl) { Content = new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json") }; // Send the request
var orgResponse = await _httpClient.SendAsync(request);
if (!orgResponse.IsSuccessStatusCode)
{
var errorResponse = await orgResponse.Content.ReadAsStringAsync();
// Step 3: Perform cleanup
await CleanupFailedOperationAsync(organizationId, userId);
throw new Exception($"Failed to add user to organization. Status: {orgResponse.StatusCode}, Response: {errorResponse}");
}
return true;
}
I’ve tried changing the request body to:
{
string[] requestBody= new[] { userId};
string requestBody= userId;
var requestBody = new {userId=userId};
List<string> requestBody = new {userId};
}
I’m using keycloak v.26.0.7 with postgresql with docker. Any help would be greatly appreciated.