client = new CloudMediaContext(new MediaServicesCredentials(accountName, accountKey));
Azure 媒体服务宣布支持 AAD 并弃用 ACS 身份验证。
因为与 ACS 令牌身份验证模型(“帐户密钥”)相比,Azure Active Directory 提供了强大的基于角色的访问控制功能并支持对帐户中资源的更细粒度的访问,我们强烈建议您在 2018 年 6 月 22 日之前更新您的代码并从 ACS 迁移到基于 AAD 的身份验证。
此外,快速迁移的一个关键原因是即将宣布弃用基于 ACS 密钥的身份验证系统。
在媒体服务中使用 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。