Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Use Azure Identity with
This article shows how to create a WebPubSubServiceClient that uses Microsoft Entra ID to authorize requests to Azure Web PubSub.
Prerequisites
- An Azure subscription.
- An existing Azure Web PubSub resource.
- A Microsoft Entra identity, such as an application or a managed identity.
Important
Assign the Microsoft Entra identity an Azure role that includes the data-plane permissions required by the SDK operations your application calls. To generate a client access token and use it to connect, the identity requires both Microsoft.SignalRService/WebPubSub/clientConnection/generateToken/action and Microsoft.SignalRService/WebPubSub/clientConnection/write. The built-in Web PubSub Service Owner role includes both permissions but grants access to all data-plane APIs. No narrower built-in role includes both permissions. For least-privilege access, use a custom role that contains only the permissions your application needs.
Choose a language:
Install the Azure Identity and Azure Web PubSub packages:
dotnet add package Azure.Identity
dotnet add package Azure.Messaging.WebPubSub
Create a DefaultAzureCredential, and pass it to WebPubSubServiceClient:
using Azure.Identity;
using Azure.Messaging.WebPubSub;
var credential = new DefaultAzureCredential();
var client = new WebPubSubServiceClient(
new Uri("<endpoint>"),
"<hub>",
credential);
For other credential types, see the Azure Identity client library for .NET.
Use dependency injection
Install the dependency injection package:
dotnet add package Microsoft.Extensions.Azure
Register WebPubSubServiceClient with the service collection:
using Azure.Identity;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.DependencyInjection;
void ConfigureServices(IServiceCollection services)
{
services.AddAzureClients(builder =>
{
builder.AddWebPubSubServiceClient(
new Uri("<endpoint>"),
"<hub>",
new DefaultAzureCredential());
});
}
For more information, see the Azure Web PubSub service client library for .NET and the complete sample.