Search code examples
azureazure-storageazure-media-services

Creating Media Service Credentials


I'm trying to set up the Azure Media Service. I created the media service from Azure but I don't know where can obtain the account key.

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

I'm following this tutorial: Integrating applications with Azure Active Directory

But after get the secret key in step 4 in "To add application credentials, or permissions to access web APIs" doesn't works.


Solution

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

    Azure Media Services announces support for AAD and deprecation of ACS authentication.

    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.

    User Authentication with AAD in Media Services

    A native application would first acquire an access token from Azure Active Directory and then use that access token to make all REST API calls.

    The following examples show how a daemon application may use AAD web application credentials to authenticate requests with the REST service.

    enter image description here

    For a REST API request to succeed, the calling user must be a “Contributor” or “Owner” of the Azure Media Services account it is trying to access.

    Users of the .NET client SDK for Media Services must upgrade to the latest version on Nuget (windowsazure.mediaservices version 4.1.0.1 or greater) to use AAD authentication for communicating with REST requests.

    You could use the following code to 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();
            }
            }
    

    NOTE: Applications will also need to update their references to include a new assembly "Microsoft.WindowsAzure.MediaServices.Client.Common.Authentication.dll" and add references to that namespace as well as reference to the "Microsoft.IdentityModel.Clients.ActiveDirectory" assembly to get access to the ITokenProvider interface.

    Click API access and choose "

    Connect to Azure Media Services API with user authentication".

    enter image description here

    This includes the API endpoint that you need to call, along with the ClientID, Domain, and Resource.

    enter image description here

    For more details about how to connect to Media Services with Azure AD, you could refer to this article.