在简书中查看,请点击我

关于相关内容解释,请参考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
    • 单击Settings,在Settings中,选择Keys
    • 在Passwords中,键入Key Description, 比如Key1,选择duration,单击Save

      输入Key1,选择duration,单击Save
    • 保存完成以后,记录下Value的值 (稍后,这个值在Desktop程序中用到,它在App.config中的变量名是AMSClientSecret)

      Client Secret
  1. 配置Media Service使用service principal连接。

    • 在All resources中,选择刚创建的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的值 (稍后,这两个信息在Desktop程序中用到,它在App.config中的变量名分别是AMSAADTenantDomain和AMSRESTAPIEndpoint)
    • 在Azure AD Application中,选择Select Existing
    • 单击Azure AD app,在Azure AD applications中,输入在步骤2中记录下的app的名字
    • 选择找到的app,并单击OK

      关联Azure AD app
    • 在Connect to Media Services API with service principal中,单击Save
  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引用
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对象
CloudMediaContext context = new CloudMediaContext(new Uri(_RESTAPIEndpoint), tokenProvider);
    • 至此,我们已经创建了一个CloudMediaContext对象context,可以使用这个对象来访问Media Service中的资源,对资源进行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>

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

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());
}
}
}

手把手:使用service principal连接Azure Media Service的更多相关文章

  1. Azure Media Service (1) 使用OBS进行Azure Media Service直播

    <Windows Azure Platform 系列文章目录> 今天正好有客户问如何使用OBS进行Azure Media Service直播,我这里简单介绍一下. 先决条件: 1. OBS ...

  2. Windows Azure Cloud Service (36) 在Azure Cloud Service配置SSL证书

    <Windows Azure Platform 系列文章目录> 在某些时候,我们需要在Azure PaaS Cloud Service配置HTTPS连接.本章将介绍如何在本地创建证书,然后 ...

  3. AAD Service Principal获取azure user list (Microsoft Graph API)

    本段代码是个通用性很强的sample code,不仅能够操作AAD本身,也能通过Azure Service Principal的授权来访问和控制Azure的订阅资源.(Azure某种程度上能看成是两个 ...

  4. 【Azure媒体服务 Azure Media Service】Azure Media Service中Stream Endpoint 说明 (流式处理终结点)

    Azure 媒体服务是一个基于云的媒体工作流平台,用于生成需要编码.打包.内容保护和直播活动广播的解决方案. 在视频的直播,点播方案中,媒体服务的架构主要由三部分构成: 推流端,把本地视频或直播内容推 ...

  5. 【应用服务 App Service】在Azure App Service中使用WebSocket - PHP的问题 - 如何使用和调用

    问题描述 在Azure App Service中,有对.Net,Java的WebSocket支持的示例代码,但是没有成功的PHP代码. 以下的步骤则是如何基于Azure App Service实现PH ...

  6. Azure Media Service

    该视频来源于Build 2015, 视频比较老, 从演讲的角度看, 是个非常不错的演讲, 内容也很全面. Apr 27, 2015

  7. 如何将Azure DevOps中的代码发布到Azure App Service中

    标题:如何将Azure DevOps中的代码发布到Azure App Service中 作者:Lamond Lu 背景 最近做了几个项目一直在用Azure DevOps和Azure App Servi ...

  8. 利用Meida Service的Java SDK来调用Azure Media Services的Index V2实现视频字幕自动识别

    Azure Media Services新的Index V2 支持自动将视频文件中的语音自动识别成字幕文件WebVtt,非常方便的就可以跟Azure Media Player集成,将一个原来没字幕的视 ...

  9. Azure登陆的两种常见方式(user 和 service principal登陆)

    通过Powershell 登陆Azure(Azure MoonCake为例)一般常见的有两种方式 1. 用户交互式登陆 前提条件:有一个AAD account 此种登陆方式会弹出一个登陆框,让你输入一 ...

随机推荐

  1. bash_profile

    export ORACLE_BASE=/home/oracle/app   export ORACLE_HOME=$ORACLE_BASE/oracle/product/11.2.0/dbhome_1 ...

  2. js中for(var key in o ){};用法小记

    o不只可以是对象,key也不只可以是对象中的键. o也可以是一个数组,这时候的key就是数组的下标,从"0"开始,注意下标“0”是个字符串类型. 但是这种循环在 IE8浏览器下 对 ...

  3. 【1】HTTP协议和Socket接口区别

    内容提要: 1.网络七层模型 2.什么是HTTP协议 3.什么是Socket接口 1.网络七层模型 第一层:物理层 为设备之间的信息提供传输提供可靠环境,那么这个环境是什么呢? 如:同轴电缆,插头,接 ...

  4. CSS:margin和padding之谜

    margin外边距,padding内边距.光看书本的介绍,理解起来好费劲,那咱就举个荔枝:你家的保险箱,是那种镶在墙壁里的,保险箱与墙壁的距离就是margin,保险箱壁就是所谓的border,保险箱与 ...

  5. NPOI 关于Excel的学习

    1.传送门:http://blog.csdn.net/guo_lover/article/details/52399570

  6. Spring MVC扩展

    使用@ResonseBody实现异步请求时返回的数据对象的输出. 通过配置StringHttpMessageConverter消息转换器来解决JSON数据传递中出现的中文乱码问题. 在实际项目开发中, ...

  7. git切换用户踩坑

    1)配置用户信息 git config --global user.name "username" git config --global user.email "**@ ...

  8. Delphi下的WinSock编程

    一.定址        要通过Winsock建立通信,必须了解如何利用指定的协议为工作站定址.Winsock 2引入了几个新的.与协议无关的函数,它们可和任何一个地址家族一起使用:但是大多数情况下,各 ...

  9. Git bash 配置多个远端仓库

    $ cat .ssh/config #aliyeye Host aliyeye.com.cn HostName aliyeye.com.cn PreferredAuthentications publ ...

  10. Angular cli 发布自定义组件

    建立工作空间 ng new Test --style=scss //Angular6.x及以下可以使用这个命令指定使用.scss样式表 ng new Test                      ...