建立使用 SignalR 服務和 GitHub 驗證的 Web 應用程式

這個範例指令碼會建立新的 Azure SignalR 服務資源,以供用來將即時內容更新推送給用戶端。 這個指令碼也會新增 Web 應用程式和 App Service 方案來裝載 ASP.NET Core Web 應用程式,以使用 SignalR Service。 Web 應用程式已設定應用程式設定以連線到新的 SignalR 服務資源,並使用 GitHub 驗證進行驗證。 Web 應用程式也會設定為使用本機 Git 存放庫部署來源。

如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。 您可以使用 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 訂用帳戶識別碼。 如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

如需詳細資訊,請參閱設定使用中訂閱以互動方式登入

使用 App Service 建立 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. 針對所需的部署用戶名稱和其passwor,更新下列腳本中的值

    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. 使用所需的部署用戶名稱和密碼更新 Webapp

    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

範例參考

下表中的每個命令都會連結至命令特定的文件。 此指令碼會使用下列命令:

Command 注意
az group create 建立用來存放所有資源的資源群組。
az signalr create 建立 Azure SignalR 服務資源。
az signalr key list 列出應用程式在使用 SignalR 推送即時內容更新時,所會使用的金鑰。
az appservice plan create 建立用來裝載 Web 應用程式的 Azure App Service 方案。
az webapp create 使用 App Service 主控方案建立 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 指令碼範例。