手把手:使用service principal连接Azure M

2018-12-06  本文已影响0人  阳光流影

关于相关内容解释,请参考docs文档 https://docs.microsoft.com/en-us/azure/media-services/previous/media-services-dotnet-get-started-with-aad
说明: 本步骤默认我们已经有Azure订阅,并且步骤是针对Global Azure,如果是China Mooncake请仅供参考。

Step by Step:

  1. 登录Azure Portal,创建Media Service服务
    • 单击All services,在搜索框中,键入Media Service


      在All services中搜做Media Service
    • 单击Media Services,在Media Services,单击+ Add


      单击+Add
    • 输入Account Name, 选择Resource Group或创建新Resource Group,选择Location,选择Storage Account,详细信息略过...
    • 单击Create
  2. 创建App
    • 在左侧服务列表中,单击Azure Active Directory


      在服务列表中,单击Azure Active Directory
    • 选择App registrations
    • 单击New application registration


      单击+New application registration
    • 输入Name, Application type选择Web app/API, Sign-on URL,随便输入一个,比如http://www.contoso.com
    • 单击Create
    • 在App registrations中, 选择刚刚创建的app
    • 记录下app的名字,Application ID等信息 (稍后,Application ID在Desktop程序中用到,它在App.config中的变量名是AMSClientId)


      Application ID
  1. 配置Media Service使用service principal连接
    • 在All resources中,选择刚创建的media service
    • 选择API access


      选择API access
  1. 创建.NET Desktop应用程序,以console Application为例,创建步骤略过
  2. 在.NET项目中,添加引用
    • 右键单击项目,选择Manage NuGet Packages


      选择Manage NuGet Packages
    • 在Browse中,输入windowsazure.mediaservices,单击Install
    • 右键单击References,选择Add Reference


      选择Add Reference
    • 搜索System.Configuration,并添加到项目中
  3. 打开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>

注意: 请使用你记录下来的值替换{ }中的内容。

  1. 打开Program.cs,添加代码
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());
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读