在簡書中查看,請點擊我。windows
關於相關內容解釋,請參考docs文檔 https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-dotnet-get-started-with-aad
說明: 本步驟默認咱們已經有Azure訂閱,而且步驟是針對Global Azure,若是是China Mooncake請僅供參考。app
單擊All services,在搜索框中,鍵入Media Servicedom
單擊Media Services,在Media Services,單擊+ Addide
在左側服務列表中,單擊Azure Active Directoryspa
單擊New application registration3d
記錄下app的名字,Application ID等信息 (稍後,Application ID在Desktop程序中用到,它在App.config中的變量名是AMSClientId)code
在Passwords中,鍵入Key Description, 好比Key1,選擇duration,單擊Savexml
保存完成之後,記錄下Value的值 (稍後,這個值在Desktop程序中用到,它在App.config中的變量名是AMSClientSecret)對象
選擇API accessblog
單擊Connect to Azure Media Services API with service principal
選擇找到的app,並單擊OK
右鍵單擊項目,選擇Manage NuGet Packages
右鍵單擊References,選擇Add Reference
<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>
注意: 請使用你記錄下來的值替換{ }中的內容。
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"];
AzureAdTokenCredentials tokenCredentials = new AzureAdTokenCredentials(_AADTenantDomain, new AzureAdClientSymmetricKey(_AMSClientId, _AMSClientSecret), AzureEnvironments.AzureCloudEnvironment); var tokenProvider = new AzureAdTokenProvider(tokenCredentials);
CloudMediaContext context = new CloudMediaContext(new Uri(_RESTAPIEndpoint), tokenProvider);
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>
注意: 請使用你記錄下來的值替換{ }中的內容。
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()); } } }