Код: Выделить всё
private async Task 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;
}
Код: Выделить всё
{
string[] requestBody= new[] { userId};
string requestBody= userId;
var requestBody = new {userId=userId};
List requestBody = new {userId};
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... esnt-exist
Мобильная версия