|
| 1 | +--- |
| 2 | +title: include file |
| 3 | +description: include file |
| 4 | +services: azure-communication-services |
| 5 | +author: dbasantes |
| 6 | + |
| 7 | +ms.service: azure-communication-services |
| 8 | +ms.subservice: azure-communication-services |
| 9 | +ms.date: 12/06/2024 |
| 10 | +ms.topic: include |
| 11 | +ms.custom: include file |
| 12 | +ms.author: dbasantes |
| 13 | +--- |
| 14 | + |
| 15 | +Get started with Azure Communication Services SMS Opt-out API by leveraging the following C# sample code. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 20 | +- The .NET Core SDK version must be higher than v6 for your operating system. |
| 21 | +- An active Communication Services resource and connection string. See [Create a Communication Services resource](https://learn.microsoft.com/azure/communication-services/quickstarts/create-communication-resource). |
| 22 | +- An SMS-enabled telephone number. See [Get a phone number](https://learn.microsoft.com/azure/communication-services/quickstarts/telephony/get-phone-number) and `allowlisted` as described in the beginning of the article. |
| 23 | + |
| 24 | +## Sample code to use Opt-Out API |
| 25 | + |
| 26 | +Below is a sample C# implementation demonstrating how to use the Opt-Out Management API to add, remove, or check opt-out entries programmatically. |
| 27 | + |
| 28 | +```cs |
| 29 | +using System.Globalization; |
| 30 | +using System.Security.Cryptography; |
| 31 | +using System.Text; |
| 32 | + |
| 33 | +// Sample for Add action. Replace with Check or Remove as necessary. |
| 34 | +async Task SendOptOutAdd(string acsResourceConnectionString, string payload) |
| 35 | +{ |
| 36 | + const string ApiPrivatePreviewVersion = "2024-12-10-preview"; |
| 37 | + |
| 38 | + const string dateHeader = "x-ms-date"; |
| 39 | + |
| 40 | + string accesskey = GetConnectionStringPart(acsResourceConnectionString, "accesskey"); |
| 41 | + var endpointUri = new Uri(GetConnectionStringPart(acsResourceConnectionString, "endpoint")); |
| 42 | + |
| 43 | + using var httpClient = new HttpClient(); |
| 44 | + httpClient.BaseAddress = endpointUri; |
| 45 | + |
| 46 | + string method = "POST"; |
| 47 | + string baseAddress = httpClient.BaseAddress.ToString().TrimEnd('/'); |
| 48 | + var requestUri = new Uri($"{baseAddress}/sms/optouts:add?api-version={ApiPrivatePreviewVersion }", UriKind.RelativeOrAbsolute); |
| 49 | + string hashedBody = ComputeSha256Hash(payload); |
| 50 | + string utcNowString = DateTimeOffset.UtcNow.ToString("r", CultureInfo.InvariantCulture); |
| 51 | + string stringToSign = $"{method}\n{requestUri.PathAndQuery}\n{utcNowString};{requestUri.Host};{hashedBody}"; |
| 52 | + string signature = ComputeHmacSha256Hash(accesskey, stringToSign); |
| 53 | + string authHeader = $"HMAC-SHA256 SignedHeaders={dateHeader};host;x-ms-content-sha256&Signature={signature}"; |
| 54 | + |
| 55 | + using HttpRequestMessage request = new(); |
| 56 | + request.Headers.TryAddWithoutValidation(dateHeader, utcNowString); |
| 57 | + request.Headers.TryAddWithoutValidation("x-ms-content-sha256", hashedBody); |
| 58 | + request.Headers.TryAddWithoutValidation("Authorization", authHeader); |
| 59 | + request.RequestUri = requestUri; |
| 60 | + request.Method = new HttpMethod(method); |
| 61 | + request.Content = new StringContent(payload, Encoding.UTF8, "application/json"); |
| 62 | + |
| 63 | + HttpResponseMessage response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); |
| 64 | + |
| 65 | + Console.WriteLine(response.StatusCode); |
| 66 | + Console.WriteLine(await response.Content.ReadAsStringAsync()); |
| 67 | + Console.WriteLine(response.Headers.ToString()); |
| 68 | +} |
| 69 | + |
| 70 | +string ComputeSha256Hash(string rawData) |
| 71 | +{ |
| 72 | + using SHA256 sha256Hash = SHA256.Create(); |
| 73 | + byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData)); |
| 74 | + return Convert.ToBase64String(bytes); |
| 75 | +} |
| 76 | + |
| 77 | +string ComputeHmacSha256Hash(string key, string rawData) |
| 78 | +{ |
| 79 | + using HMACSHA256 hmacSha256 = new HMACSHA256(Convert.FromBase64String(key)); |
| 80 | + byte[] bytes = hmacSha256.ComputeHash(Encoding.ASCII.GetBytes(rawData)); |
| 81 | + return Convert.ToBase64String(bytes); |
| 82 | +} |
| 83 | + |
| 84 | +string GetConnectionStringPart(string acsResourceConnectionString, string key) |
| 85 | +{ |
| 86 | + return acsResourceConnectionString.Split($"{key}=").Last().Split(';').First(); |
| 87 | +} |
| 88 | + |
| 89 | +// Usage |
| 90 | +
|
| 91 | +const string ConnectionString = "endpoint=https://[CONTOSO].communication.azure.com/;accesskey=******"; |
| 92 | +var payload = System.Text.Json.JsonSerializer.Serialize(new |
| 93 | +{ |
| 94 | + from = "+15551234567", //replace with your allowed sender number |
| 95 | + recipients = new[] { |
| 96 | + new { to = "+15550112233" } //replace with your recipient |
| 97 | + }, |
| 98 | +}); |
| 99 | + |
| 100 | +await SendOptOutAdd(ConnectionString, payload); |
| 101 | + |
| 102 | +``` |
| 103 | + |
0 commit comments