手把手:使用service principal連接Azure Media Service

關(guān)于相關(guān)內(nèi)容解釋,請(qǐng)參考docs文檔 https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-dotnet-get-started-with-aad
說(shuō)明: 本步驟默認(rèn)我們已經(jīng)有Azure訂閱,并且步驟是針對(duì)Global Azure,如果是China Mooncake請(qǐng)僅供參考。

Step by Step:

  1. 登錄Azure Portal,創(chuàng)建Media Service服務(wù)。
    • 單擊All services,在搜索框中,鍵入Media Service


      在All services中搜做Media Service
    • 單擊Media Services,在Media Services,單擊+ Add


      單擊+Add
    • 輸入Account Name, 選擇Resource Group或創(chuàng)建新Resource Group,選擇Location,選擇Storage Account,詳細(xì)信息略過(guò)...
    • 單擊Create
  2. 創(chuàng)建App。
    • 在左側(cè)服務(wù)列表中,單擊Azure Active Directory


      在服務(wù)列表中,單擊Azure Active Directory
    • 選擇App registrations
    • 單擊New application registration


      單擊+New application registration
    • 輸入Name, Application type選擇Web app/API, Sign-on URL,隨便輸入一個(gè),比如http://www.contoso.com
    • 單擊Create
    • 在App registrations中, 選擇剛剛創(chuàng)建的app
    • 記錄下app的名字,Application ID等信息 (稍后,Application ID在Desktop程序中用到,它在App.config中的變量名是AMSClientId)


      Application ID
  • 單擊Settings,在Settings中,選擇Keys
  • 在Passwords中,鍵入Key Description, 比如Key1,選擇duration,單擊Save


    輸入Key1,選擇duration,單擊Save
  • 保存完成以后,記錄下Value的值 (稍后,這個(gè)值在Desktop程序中用到,它在App.config中的變量名是AMSClientSecret)


    Client Secret
  1. 配置Media Service使用service principal連接。
    • 在All resources中,選擇剛創(chuàng)建的media service
    • 選擇API access


      選擇API access
  • 單擊Connect to Azure Media Services API with service principal


    單擊Connect to Azure Media Services API with service principal
  • 記錄下Azure Active Directory tenant domainREST API endpoint的值 (稍后,這兩個(gè)信息在Desktop程序中用到,它在App.config中的變量名分別是AMSAADTenantDomain和AMSRESTAPIEndpoint)
  • 在Azure AD Application中,選擇Select Existing
  • 單擊Azure AD app,在Azure AD applications中,輸入在步驟2中記錄下的app的名字
  • 選擇找到的app,并單擊OK


    關(guān)聯(lián)Azure AD app
  • 在Connect to Media Services API with service principal中,單擊Save
  1. 創(chuàng)建.NET Desktop應(yīng)用程序,以console Application為例,創(chuàng)建步驟略過(guò)。
  2. 在.NET項(xiàng)目中,添加引用。
    • 右鍵單擊項(xiàng)目,選擇Manage NuGet Packages


      選擇Manage NuGet Packages
    • 在Browse中,輸入windowsazure.mediaservices,單擊Install
    • 右鍵單擊References,選擇Add Reference


      選擇Add Reference
    • 搜索System.Configuration,并添加到項(xiàng)目中
  3. 打開(kāi)App.config文件,添加如下代碼
  <appSettings>
    <add key="AMSAADTenantDomain" value="{your AAD Tenant Domain}"/>
    <add key="AMSRESTAPIEndpoint" value="{your REST API Endpoint}"/>
    <add key="AMSClientId" value="{your Application ID}"/>
    <add key="AMSClientSecret" value="{your Client secret}"/>
  </appSettings>

注意: 請(qǐng)使用你記錄下來(lái)的值替換{ }中的內(nèi)容。

  1. 打開(kāi)Program.cs,添加代碼
  • 添加using引用
using Microsoft.WindowsAzure.MediaServices.Client;
using System.Configuration;
  • 定義變量
private static readonly string _AADTenantDomain = ConfigurationManager.AppSettings["AMSAADTenantDomain"];
private static readonly string _RESTAPIEndpoint = ConfigurationManager.AppSettings["AMSRESTAPIEndpoint"];
private static readonly string _AMSClientId = ConfigurationManager.AppSettings["AMSClientId"];
private static readonly string _AMSClientSecret = ConfigurationManager.AppSettings["AMSClientSecret"];
  • 定義tokenCredentials變量和tokenProvide變量
AzureAdTokenCredentials tokenCredentials = new AzureAdTokenCredentials(_AADTenantDomain,
                new AzureAdClientSymmetricKey(_AMSClientId, _AMSClientSecret),
                AzureEnvironments.AzureCloudEnvironment);
var tokenProvider = new AzureAdTokenProvider(tokenCredentials);
  • 定義CloudMediaContext對(duì)象
CloudMediaContext context = new CloudMediaContext(new Uri(_RESTAPIEndpoint), tokenProvider);
  • 至此,我們已經(jīng)創(chuàng)建了一個(gè)CloudMediaContext對(duì)象context,可以使用這個(gè)對(duì)象來(lái)訪問(wèn)Media Service中的資源,對(duì)資源進(jìn)行Encode,publish等操作。

附錄代碼

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
  <appSettings>
    <add key="AMSAADTenantDomain" value="{your AAD Tenant Domain}"/>
    <add key="AMSRESTAPIEndpoint" value="{your REST API Endpoint}"/>
    <add key="AMSClientId" value="{your Application ID}"/>
    <add key="AMSClientSecret" value="{your Client secret}"/>
  </appSettings>
</configuration>

注意: 請(qǐng)使用你記錄下來(lái)的值替換{ }中的內(nèi)容。

Program.cs

using System;
using System.Linq;
using Microsoft.WindowsAzure.MediaServices.Client;
using System.Configuration;

namespace ConsoleApp4
{
    class Program
    {
        private static readonly string _AADTenantDomain =
    ConfigurationManager.AppSettings["AMSAADTenantDomain"];
        private static readonly string _RESTAPIEndpoint =
            ConfigurationManager.AppSettings["AMSRESTAPIEndpoint"];
        private static readonly string _AMSClientId =
            ConfigurationManager.AppSettings["AMSClientId"];
        private static readonly string _AMSClientSecret =
            ConfigurationManager.AppSettings["AMSClientSecret"];
        static void Main(string[] args)
        {
            AzureAdTokenCredentials tokenCredentials = new AzureAdTokenCredentials(_AADTenantDomain,
                new AzureAdClientSymmetricKey(_AMSClientId, _AMSClientSecret),
                AzureEnvironments.AzureCloudEnvironment);
            var tokenProvider = new AzureAdTokenProvider(tokenCredentials);
            CloudMediaContext context = new CloudMediaContext(new Uri(_RESTAPIEndpoint), tokenProvider);

            var assets = context.Assets;
            foreach (var item in assets)
            {
                Console.WriteLine(item.Name);
            }
            Console.ReadLine();
            Console.WriteLine(context.StorageAccounts.First().Name.ToString());
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容