创建使用 SignalR 服务和 GitHub 身份验证的 Web 应用

此示例脚本会创建新的 Azure SignalR 服务资源,用于将实时内容更新推送到客户端。 此脚本还会添加新的 Web 应用和应用服务计划,以托管使用 SignalR 服务的 ASP.NET Core Web 应用。 使用应用设置将 Web 应用配置为连接到新的 SignalR 服务资源,并使用 GitHub 身份验证进行身份验证。 Web 应用还配置为使用本地 Git 存储库部署资源。

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅

示例脚本

登录 Azure

使用以下脚本通过其他订阅登录,将 <Subscription ID> 替换为 Azure 订阅 ID。 如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅

az cloud set -n AzureChinaCloud
az login

subscription="<subscriptionId>" # add subscription 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="China East 2"
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 部署

  1. 更新以下脚本中所需的部署用户名及其密码对应的值

    deploymentUser=<Replace with your desired username>
    deploymentUserPassword=<Replace with your desired password>
    
  2. 根据 GitHub OAuth 应用注册更新以下脚本中的值。

    GitHubClientId=<Replace with your GitHub OAuth app Client ID>
    GitHubClientSecret=<Replace with your GitHub OAuth app Client Secret>
    
  3. 添加用于 GitHub 身份验证的应用设置

    az webapp config appsettings set --name $webApp --resource-group $resourceGroup --settings "GitHubClientSecret=$GitHubClientSecret" 
    
  4. 使用所需的部署用户名和密码更新 Web 应用

    az webapp deployment user set --user-name $deploymentUser --password $deploymentUserPassword
    
  5. 配置 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 appservice plan create 创建用于托管 Web 应用的 Azure 应用服务计划。
az webapp create 使用应用服务托管计划创建 Azure Web 应用。
az webapp config appsettings set 为 Web 应用添加新的应用设置。 这些应用设置用于存储 SignalR 连接字符串和 GitHub OAuth 应用密钥。
az webapp deployment user set 更新部署凭据。
az webapp deployment source config-local-git 获取 git 存储库终结点的 URL 用于为 web 应用部署克隆和推送。

后续步骤

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档

可在 Azure SignalR 服务文档中找到其他 Azure SignalR 服务 CLI 脚本示例。