你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

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

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

如果没有 Azure 订阅,请在开始之前创建一个 Azure 免费帐户

Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。 可以将 Bash 或 PowerShell 与 Cloud Shell 配合使用来使用 Azure 服务。 可以使用 Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。

若要启动 Azure Cloud Shell,请执行以下操作:

选项 示例/链接
选择代码或命令块右上角的“试用”。 选择“试用”不会自动将代码或命令复制到 Cloud Shell。 Screenshot that shows an example of Try It for Azure Cloud Shell.
转到 https://shell.azure.com 或选择启动 Cloud Shell 按钮可在浏览器中打开 Cloud Shell。 Button to launch Azure Cloud Shell.
选择 Azure 门户右上角菜单栏上的 Cloud Shell 按钮。 Screenshot that shows the Cloud Shell button in the Azure portal

若要使用 Azure Cloud Shell,请执行以下操作:

  1. 启动 Cloud Shell。

  2. 选择代码块(或命令块)上的“复制”按钮以复制代码或命令。

  3. 在 Windows 和 Linux 上选择 Ctrl+Shift+V,或在 macOS 上选择 Cmd+Shift+V 将代码或命令粘贴到 Cloud Shell 会话中。

  4. 选择“Enter”运行代码或命令。

示例脚本

启动 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 会在登录时使用的初始帐户下自动进行身份验证。 使用以下脚本通过其他订阅登录,将 <Subscription ID> 替换为 Azure 订阅 ID。 如果没有 Azure 订阅,请在开始之前创建一个 Azure 免费帐户

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="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 部署

  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 脚本示例。