Use the Content Moderation .NET SDK to add a person to one or more person groups.
For the full list of parameters, see API operation for adding a person to a group.
Prerequisites
Before you begin, ensure that you have:
Installed the .NET dependencies using the required .NET version. Using an unsupported version causes subsequent API calls to fail. See Install .NET dependencies for instructions and version requirements.
Configured the AI Guardrails API endpoint. See Endpoint for the endpoint URL for your region.
Add a person using the .NET SDK
The following example adds a person to two groups in a single request. The personId and groupIds parameters are both required.
using System;
using Newtonsoft.Json;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Green.Model.V20180509;
using System.Collections.Generic;
namespace csharp_sdk_sample
{
class Program
{
static void Main(string[] args)
{
// Load credentials from environment variables to avoid hardcoding sensitive values.
DefaultProfile profile = DefaultProfile.GetProfile(
"cn-shanghai",
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// Reuse the client instance across requests to improve performance
// and avoid repeated connection overhead.
DefaultAcsClient client = new DefaultAcsClient(profile);
AddGroupsRequest request = new AddGroupsRequest();
request.AcceptFormat = FormatType.JSON;
request.ContentType = FormatType.JSON;
request.Method = MethodType.POST;
request.Encoding = "UTF-8";
Dictionary<string, object> httpBody = new Dictionary<string, object>();
// personId: the ID assigned to the person when they were created.
httpBody.Add("personId", "<person-id>");
// groupIds: the IDs of the groups to add the person to.
// A person can belong to multiple groups simultaneously.
httpBody.Add("groupIds", new List<string> { "<group-id-1>", "<group-id-2>" });
request.SetContent(
System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(httpBody)),
"utf-8",
FormatType.JSON);
try
{
AddGroupsResponse response = client.GetAcsResponse(request);
if (response.HttpResponse.Status != 200)
{
Console.WriteLine("Request failed. HTTP status: {0}", response.HttpResponse.Status);
}
else
{
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
}
catch (Exception ex)
{
Console.WriteLine("Request failed: {0}", ex.Message);
}
}
}
}Replace the following placeholders with actual values:
| Placeholder | Description |
|---|---|
<person-id> | The ID of the person to add. This is the ID assigned when the person was created. |
<group-id-1>, <group-id-2> | The IDs of the target person groups. Include as many group IDs as needed. |
What's next
API operation for adding a person to a group — full parameter reference and error codes