你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
This package contains common code for Azure Communication Service libraries.
Source code | Package (NuGet) | Product documentation
Getting started
Install the package
Install the Azure Communication Common client library for .NET with NuGet.
dotnet add package Azure.Communication.Common
Prerequisites
You need an Azure subscription and a Communication Service Resource to use this package.
To create a new Communication Service, you can use the Azure Portal, the Azure PowerShell, or the .NET management client library.
Authenticate the client
This module does not contain a client and instead libraries that help other Azure Communication clients authenticate.
Key concepts
CommunicationTokenCredential
The CommunicationTokenCredential
object is used to authenticate a user with Communication Services, such as Chat or Calling. It optionally provides an auto-refresh mechanism to ensure a continuously stable authentication state during communications.
Depending on your scenario, you may want to initialize the CommunicationTokenCredential
with:
- a static token (suitable for short-lived clients used to e.g. send one-off Chat messages) or
- a callback function that ensures a continuous authentication state (ideal e.g. for long Calling sessions).
- a token credential capable of obtaining an Entra user token. You can provide any implementation of Azure.Core.TokenCredential. It is suitable for scenarios where Entra user access tokens are needed to authenticate with Communication Services.
The tokens supplied to the CommunicationTokenCredential
either through the constructor or via the token refresher callback can be obtained using the Azure Communication Identity library.
Thread safety
We guarantee that all client instance methods are thread-safe and independent of each other (guideline). This ensures that the recommendation of reusing client instances is always safe, even across threads.
Additional concepts
Client options | Accessing the response | Long-running operations | Handling failures | Diagnostics | Mocking | Client lifetime
Examples
Create a credential with a static token
For short-lived clients, refreshing the token upon expiry is not necessary and CommunicationTokenCredential
may be instantiated with a static token.
string token = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_USER_TOKEN");
using var tokenCredential = new CommunicationTokenCredential(token);
Create a credential with a callback
Alternatively, for long-lived clients, you can create a CommunicationTokenCredential
with a callback to renew tokens if expired.
Here we pass two imagined functions that make network requests to retrieve token strings for user Bob.
If callbacks are passed, upon requests (sending a chat message), CommunicationTokenCredential
ensures
that a valid token is acquired prior to executing the request.
It's necessary that the FetchTokenForUserFromMyServer
method returns a valid token (with an expiration date set in the future) at all times.
Optionally, you can enable proactive token refreshing where a fresh token will be acquired as soon as the previous token approaches expiry. Using this method, your requests are less likely to be blocked to acquire a fresh token:
using var tokenCredential = new CommunicationTokenCredential(
new CommunicationTokenRefreshOptions(
refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand
tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken))
{
AsyncTokenRefresher = cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken)
});
If you already have a token, you can optimize the token refreshing even further by passing that initial token:
string initialToken = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_USER_TOKEN");
using var tokenCredential = new CommunicationTokenCredential(
new CommunicationTokenRefreshOptions(
refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand
tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("bob@contoso.com", cancellationToken))
{
AsyncTokenRefresher = cancellationToken => FetchTokenForUserFromMyServerAsync("bob@contoso.com", cancellationToken),
InitialToken = initialToken
});
Create a credential with a token credential capable of obtaining an Entra user token
For scenarios where an Entra user can be used with Communication Services, you need to initialize any implementation of Azure.Core.TokenCredential and provide it to the EntraCommunicationTokenCredentialOptions
.
Along with this, you must provide the URI of the Azure Communication Services resource and the scopes required for the Entra user token. These scopes determine the permissions granted to the token.
This approach needs to be used for authorizing an Entra user with a Teams license to use Teams Phone Extensibility features through your Azure Communication Services resource.
This requires providing the https://auth.msft.communication.azure.com/TeamsExtension.ManageCalls
scope.
var options = new InteractiveBrowserCredentialOptions
{
TenantId = "<your-tenant-id>",
ClientId = "<your-client-id>",
RedirectUri = new Uri("<your-redirect-uri>")
};
var entraTokenCredential = new InteractiveBrowserCredential(options);
var entraTokenCredentialOptions = new EntraCommunicationTokenCredentialOptions(
resourceEndpoint: "https://<your-resource>.communication.azure.com",
entraTokenCredential: entraTokenCredential)
)
{
Scopes = new[] { "https://auth.msft.communication.azure.com/TeamsExtension.ManageCalls" }
};
var credential = new CommunicationTokenCredential(entraTokenCredentialOptions);
Other scenarios for Entra users to utilize Azure Communication Services are currently in the preview stage only and should not be used in production.
The scopes for these scenarios follow the format https://communication.azure.com/clients/<Azure Communication Services Clients API permission>
.
If specific scopes are not provided, the default scopes will be set to https://communication.azure.com/clients/.default
.
var options = new InteractiveBrowserCredentialOptions
{
TenantId = "<your-tenant-id>",
ClientId = "<your-client-id>",
RedirectUri = new Uri("<your-redirect-uri>")
};
var entraTokenCredential = new InteractiveBrowserCredential(options);
var entraTokenCredentialOptions = new EntraCommunicationTokenCredentialOptions(
resourceEndpoint: "https://<your-resource>.communication.azure.com",
entraTokenCredential: entraTokenCredential)
{
Scopes = new[] { "https://communication.azure.com/clients/VoIP" }
};
var credential = new CommunicationTokenCredential(entraTokenCredentialOptions);
Troubleshooting
The proactive refreshing failures happen in a background thread and to avoid crashing your app the exceptions will be silently handled.
All the other failures will happen during your request using other clients such as chat where you can catch the exception using RequestFailedException
.
Next steps
Read more about Communication user access tokens
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.