创建媒体服务凭证

Creating Media Service Credentials

我正在尝试设置 Azure 媒体服务。我从 Azure 创建了媒体服务,但我不知道在哪里可以获得帐户密钥。

client = new CloudMediaContext(new MediaServicesCredentials(accountName, accountKey));

我正在学习本教程:Integrating applications with Azure Active Directory

但是在“添加应用程序凭据或访问 Web API 的权限”中的第 4 步中获取密钥后不起作用。

client = new CloudMediaContext(new MediaServicesCredentials(accountName, accountKey));

Azure 媒体服务宣布支持 AAD 并弃用 ACS 身份验证

Because Azure Active Directory provides powerful role-based access control features and support for more fine-grained access to resources in your account compared to the ACS token authentication model ("account keys"), we strongly recommend that you update your code and migrate from ACS to AAD-based authentication by June 22, 2018.

Also, a key reason for the rapid migration is the upcoming announced deprecation of the ACS key based authentication system.

在媒体服务中使用 AAD 进行用户身份验证

本机应用程序将首先从 Azure Active Directory 获取访问令牌,然后使用该访问令牌进行所有 REST API 调用。

以下示例展示了守护程序应用程序如何使用 AAD Web 应用程序凭据对 REST 服务请求进行身份验证。

要使 REST API 请求成功,调用用户必须是它尝试访问的 Azure 媒体服务帐户的 “贡献者”或“所有者”使用权。

用于媒体服务的 .NET 客户端 SDK 的用户必须在 Nuget 上升级到最新版本(windowsazure.mediaservices 版本 4.1.0.1 或更高版本)才能使用 AAD 身份验证用于与 REST 请求通信。

您可以使用以下代码 connect to the Media Services account

class Program
    {
        // Read values from the App.config file.
        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"];

        private static CloudMediaContext _context = null;

        static void Main(string[] args)
        {
        try
        {
            AzureAdTokenCredentials tokenCredentials = 
                new AzureAdTokenCredentials(_AADTenantDomain,
                    new AzureAdClientSymmetricKey(_AMSClientId, _AMSClientSecret),
                    AzureEnvironments.AzureCloudEnvironment);

            var tokenProvider = new AzureAdTokenProvider(tokenCredentials);

            _context = new CloudMediaContext(new Uri(_RESTAPIEndpoint), tokenProvider);

            // Add calls to methods defined in this section.
            // Make sure to update the file name and path to where you have your media file.
            IAsset inputAsset =
            UploadFile(@"C:\VideoFiles\BigBuckBunny.mp4", AssetCreationOptions.None);

            IAsset encodedAsset =
            EncodeToAdaptiveBitrateMP4s(inputAsset, AssetCreationOptions.None);

            PublishAssetGetURLs(encodedAsset);
        }
        catch (Exception exception)
        {
            // Parse the XML error message in the Media Services response and create a new
            // exception with its content.
            exception = MediaServicesExceptionParser.Parse(exception);

            Console.Error.WriteLine(exception.Message);
        }
        finally
        {
            Console.ReadLine();
        }
        }

注意:应用程序还需要更新它们的引用以包含新程序集“Microsoft.WindowsAzure.MediaServices.Client.Common.Authentication.dll”并添加对该命名空间的引用以及对“ Microsoft.IdentityModel.Clients.ActiveDirectory" 程序集以访问 ITokenProvider 接口。

点击API访问并选择“

使用用户身份验证连接到 Azure 媒体服务API。

这包括您需要调用的 API 端点,以及 ClientID、域和资源。

有关如何使用 Azure AD 连接到媒体服务的更多详细信息,您可以参考此article