在简书中查看,请点击我。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()); } } }