创建使用 SignalR 服务和 GitHub 身份验证的 Web 应用Create a web app that uses SignalR Service and GitHub authentication

此示例脚本会创建新的 Azure SignalR 服务资源,用于将实时内容更新推送到客户端。This sample script creates a new Azure SignalR Service resource, which is used to push real-time content updates to clients. 此脚本还会添加新的 Web 应用和应用服务计划,以托管使用 SignalR 服务的 ASP.NET Core Web 应用。This script also adds a new Web App and App Service plan to host your ASP.NET Core Web App that uses the SignalR Service. 使用应用设置将 Web 应用配置为连接到新的 SignalR 服务资源,并使用 GitHub 身份验证进行身份验证。The web app is configured with app settings to connect to the new SignalR service resource, and authenticate with GitHub authentication. Web 应用还配置为使用本地 Git 存储库部署资源。The web app is also configured to use a local git repository deployment source.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

如果选择在本地安装并使用 CLI,本文要求运行 Azure CLI 2.0 版或更高版本。If you choose to install and use the CLI locally, this article requires that you are running the Azure CLI version 2.0 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

示例脚本Sample script

此脚本使用适用于 Azure CLI 的 signalr 扩展 。This script uses the signalr extension for the Azure CLI. 使用此示例脚本前,执行以下命令,安装适用于 Azure CLI 的 signalr 扩展 :Execute the following command to install the signalr extension for the Azure CLI before using this sample script:

#!/bin/bash

#========================================================================
#=== Update these values based on your desired deployment username    ===
#=== and password.                                                    ===
#========================================================================
deploymentUser=<Replace with your desired username>
deploymentUserPassword=<Replace with your desired password>

#========================================================================
#=== Update these values based on your GitHub OAuth App registration. ===
#========================================================================
GitHubClientId=<Replace with your GitHub OAuth app Client ID>
GitHubClientSecret=<Replace with your GitHub OAuth app Client Secret>


# Generate a unique suffix for the service name
let randomNum=$RANDOM*$RANDOM

# Generate unique names for the SignalR service, resource group, 
# app service, and app service plan
SignalRName=SignalRTestSvc$randomNum
#resource name must be lowercase
mySignalRSvcName=${SignalRName,,}
myResourceGroupName=$SignalRName"Group"
myWebAppName=SignalRTestWebApp$randomNum
myAppSvcPlanName=$myAppSvcName"Plan"

# Create resource group 
az group create --name $myResourceGroupName --location chinaeast2

# Create the Azure SignalR Service resource
az signalr create \
  --name $mySignalRSvcName \
  --resource-group $myResourceGroupName \
  --sku Standard_S1 \
  --unit-count 1 \
  --service-mode Default

# Create an App Service plan.
az appservice plan create --name $myAppSvcPlanName --resource-group $myResourceGroupName --sku FREE

# Create the Web App
az webapp create --name $myWebAppName --resource-group $myResourceGroupName --plan $myAppSvcPlanName  

# Get the SignalR primary connection string
primaryConnectionString=$(az signalr key list --name $mySignalRSvcName \
  --resource-group $myResourceGroupName --query primaryConnectionString -o tsv)

#Add an app setting to the web app for the SignalR connection
az webapp config appsettings set --name $myWebAppName --resource-group $myResourceGroupName \
  --settings "Azure:SignalR:ConnectionString=$primaryConnectionString" 

#Add app settings to use with GitHub authentication
az webapp config appsettings set --name $myWebAppName --resource-group $myResourceGroupName \
  --settings "GitHubClientId=$GitHubClientId" 
az webapp config appsettings set --name $myWebAppName --resource-group $myResourceGroupName \
  --settings "GitHubClientSecret=$GitHubClientSecret" 

# Add the desired deployment user name and password
az webapp deployment user set --user-name $deploymentUser --password $deploymentUserPassword

# Configure Git deployment and note the deployment URL in the output
az webapp deployment source config-local-git --name $myWebAppName --resource-group $myResourceGroupName \
  --query [url] -o tsv

记下为新资源组生成的实际名称。Make a note of the actual name generated for the new resource group. 它将在输出中显示。It will be shown in the output. 如果要删除所有组资源,将使用该资源组名称。You will use that resource group name when you want to delete all group resources.

清理部署Clean up deployment

运行示例脚本后,可以使用以下命令删除资源组以及与其关联的所有资源。After the sample script has been run, the following command can be used to remove the resource group and all resources associated with it.

az group delete --name myResourceGroup

脚本说明Script explanation

表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation. 此脚本使用以下命令:This script uses the following commands:

CommandCommand 说明Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az signalr createaz signalr create 创建 Azure SignalR 服务资源。Creates an Azure SignalR Service resource.
az signalr key listaz signalr key list 列出密钥,使用 SignalR 推送实时内容更新时,应用程序将使用这些密钥。List the keys, which will be used by your application when pushing real-time content updates with SignalR.
az appservice plan createaz appservice plan create 创建用于托管 Web 应用的 Azure 应用服务计划。Creates an Azure App Service Plan for hosting web apps.
az webapp createaz webapp create 使用应用服务托管计划创建 Azure Web 应用。Creates an Azure Web app using the App Service hosting plan.
az webapp config appsettings setaz webapp config appsettings set 为 Web 应用添加新的应用设置。Adds new app settings for the web app. 这些应用设置用于存储 SignalR 连接字符串和 GitHub OAuth 应用密钥。These app settings are used to store the SignalR connection string and GitHub OAuth app secrets.
az webapp deployment user setaz webapp deployment user set 更新部署凭据。Update deployment credentials.
az webapp deployment source config-local-gitaz webapp deployment source config-local-git 获取 git 存储库终结点的 URL 用于为 web 应用部署克隆和推送。Get a URL for a git repository endpoint to clone and push to for web app deployment.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可在 Azure SignalR 服务文档中找到其他 Azure SignalR 服务 CLI 脚本示例。Additional Azure SignalR Service CLI script samples can be found in the Azure SignalR Service documentation.