此示例脚本会创建新的 Azure SignalR 服务资源,用于将实时内容更新推送到客户端。 此脚本还会添加新的 Web 应用和应用服务计划,以托管使用 SignalR 服务的 ASP.NET Core Web 应用。 使用应用设置将 Web 应用配置为连接到新的 SignalR 服务资源,并使用 GitHub 身份验证进行身份验证。 Web 应用还配置为使用本地 Git 存储库部署资源。
重要
本文中出现的原始连接字符串仅用于演示目的。
连接字符串包括应用程序访问 Azure SignalR 服务所需的授权信息。 连接字符串中的访问密钥类似于服务的根密码。 在生产环境中,请始终保护访问密钥。 使用 Azure 密钥保管库安全地管理和轮换密钥,使用 Microsoft Entra ID 保护连接字符串,并使用 Microsoft Entra ID 授权访问。
避免将访问密钥分发给其他用户、对其进行硬编码或将其以纯文本形式保存在其他人可以访问的任何位置。 如果你认为访问密钥可能已泄露,请轮换密钥。
如果没有 Azure 帐户,请在开始前创建一个免费帐户。
Azure Cloud Shell
Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。 可以将 Bash 或 PowerShell 与 Cloud Shell 配合使用来使用 Azure 服务。 可以使用 Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。
若要启动 Azure Cloud Shell,请执行以下操作:
选项 | 示例/链接 |
---|---|
选择代码或命令块右上角的“试用”。 选择“试用”不会自动将代码或命令复制到 Cloud Shell。 | ![]() |
转到 https://shell.azure.com 或选择“启动 Cloud Shell”按钮可在浏览器中打开 Cloud Shell。 | ![]() |
选择 Azure 门户右上角菜单栏上的 Cloud Shell 按钮。 | ![]() |
若要使用 Azure Cloud Shell,请执行以下操作:
启动 Cloud Shell。
选择代码块(或命令块)上的“复制”按钮以复制代码或命令。
在 Windows 和 Linux 上选择 CtrlShift+V,或在 macOS 上选择 CmdShift+V 将代码或命令粘贴到 Cloud Shell 会话中。
选择“Enter”运行代码或命令。
示例脚本
本文中出现的原始连接字符串仅用于演示目的。 在生产环境中,请始终保护访问密钥。 使用 Azure 密钥保管库安全地管理和轮换密钥,使用 Microsoft Entra ID 保护连接字符串,并使用 Microsoft Entra ID 授权访问。
启动 Azure Cloud Shell
Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。 它预安装有常用 Azure 工具并将其配置与帐户一起使用。
若要打开 Cloud Shell,只需要从代码块的右上角选择“试一试”。 也可以通过转到 https://shell.azure.com 在单独的浏览器标签页中启动 Cloud Shell。
当 Cloud Shell 打开时,请验证是否为环境选择了“Bash”。 后续会话将在 Bash 环境中使用 Azure CLI,选择“复制”以复制代码块,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。
登录 Azure
Cloud Shell 会在登录时使用的初始帐户下自动进行身份验证。 使用以下脚本通过其他订阅登录,将 subscriptionId 替换为你的 Azure 订阅 ID。
如果没有 Azure 帐户,请在开始前创建一个免费帐户。
subscription="subscriptionId" # Set Azure subscription ID here
az account set -s $subscription # ...or use 'az login'
使用应用服务创建 SignalR 服务
# Create a SignalR Service with an App Service
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
___location="East US"
resourceGroup="msdocs-azure-signalr-rg-$randomIdentifier"
tag="create-signal-service-with-app-service"
signalRSvc="msdocs-signalr-svc-$randomIdentifier"
webApp="msdocs-web-app-signalr-$randomIdentifier"
appSvcPlan="msdocs-app-svc-plan-$randomIdentifier"
signalRSku="Standard_S1"
unitCount="1"
serviceMode="Default"
planSku="Free"
# Create a resource group
echo "Creating $resourceGroup in "$___location"..."
az group create --name $resourceGroup --___location "$___location" --tag $tag
# Create the Azure SignalR Service resource
echo "Creating $signalRSvc"
az signalr create \
--name $signalRSvc \
--resource-group $resourceGroup \
--sku $signalRSku \
--unit-count $unitCount \
--service-mode $serviceMode
# Create an App Service plan.
echo "Creating $appSvcPlan"
az appservice plan create --name $appSvcPlan --resource-group $resourceGroup --sku $planSku
# Create the Web App
echo "Creating $webApp"
az webapp create --name $webApp --resource-group $resourceGroup --plan $appSvcPlan
# Get the SignalR primary connection string
primaryConnectionString=$(az signalr key list --name $signalRSvc \
--resource-group $resourceGroup --query primaryConnectionString -o tsv)
echo $primaryConnectionString
# Add an app setting to the web app for the SignalR connection
az webapp config appsettings set --name $webApp --resource-group $resourceGroup \
--settings "AzureSignalRConnectionString=$primaryConnectionString"
为 Web 应用启用 GitHub 身份验证和 Git 部署
根据 GitHub OAuth 应用注册更新以下脚本中的值。
GitHubClientId=<Replace with your GitHub OAuth app Client ID> GitHubClientSecret=<Replace with your GitHub OAuth app Client Secret>
添加用于 GitHub 身份验证的应用设置
az webapp config appsettings set --name $webApp --resource-group $resourceGroup --settings "GitHubClientSecret=$GitHubClientSecret"
配置 Git 部署并返回部署 URL。
az webapp deployment source config-local-git --name $webAppName --resource-group $resourceGroupName --query [url] -o tsv
清理资源
使用 az group delete 命令删除资源组以及与其关联的所有资源 - 除非你持续需要这些资源。 其中一些资源在创建和删除时可能要稍等片刻。
az group delete --name $resourceGroup
示例参考
表中的每条命令均链接到特定于命令的文档。 此脚本使用以下命令:
命令 | 说明 |
---|---|
az group create命令用于创建资源组 | 创建用于存储所有资源的资源组。 |
az signalr create | 创建 Azure SignalR 服务资源。 |
az signalr key list | 列出密钥,使用 SignalR 推送实时内容更新时,应用程序将使用这些密钥。 |
az 应用服务计划 创建 | 创建用于托管 Web 应用的 Azure 应用服务计划。 |
az webapp create命令用于创建Web应用程序。 | 使用应用服务托管计划创建 Azure Web 应用。 |
az webapp 配置 应用设置 设置 | 为 Web 应用添加新的应用设置。 这些应用设置用于存储 SignalR 连接字符串和 GitHub OAuth 应用密钥。 |
az webapp 部署用户设置 | 更新部署凭据。 |
az webapp deployment source config-local-git(将本地Git仓库配置为Web应用程序的部署源) | 获取 git 存储库终结点的 URL 用于为 web 应用部署克隆和推送。 |
后续步骤
有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档。
可在 Azure SignalR 服务文档中找到其他 Azure SignalR 服务 CLI 脚本示例。