如何播放通过代码动态获取的资产的安全流式URL(在Azure媒体服务中)

时间:2012-12-27 09:37:24

标签: azure-media-services

我正在使用Azure媒体服务和Silverlight播放器来播放流媒体网址

我能够将视频文件摄取,编码为资产文件,但是当我播放流式网址时,我遇到了问题。

我使用以下代码来获取网址...

context = new CloudMediaContext(_accountName, _accountKey);
 IAsset myAsset = GetAsset("UUID:7a32b941-30bd-4c96-bf4e-26df5022eec5");
 var theManifest = from f in myAsset.AssetFiles
 where f.Name.EndsWith(".ism")
 select f;
 var manifestFile = theManifest.First();
 IAccessPolicy streamingPolicy = _context.AccessPolicies.Create("Streaming policy",
 TimeSpan.FromDays(10),
 AccessPermissions.Read);
 ILocator originLocator = _context.Locators.CreateSasLocator(myAsset, streamingPolicy, DateTime.UtcNow.AddMinutes(-500));
 GetAssetSasUrlList(myAsset, originLocator);
 string urlForClientStreaming = originLocator.Path + manifestFile.Name + "/manifest";
 Console.WriteLine("URL to manifest for client streaming: ");
 Console.WriteLine(urlForClientStreaming);

这个网址就像 -

https://mediasvc06w4dq5k8vd08.blob.core.windows.net/asset-064ed2d5-e42d-4c49-98eb-a712db5c614f?st=2012-12-26T23%3A04%3A22Z&se=2013-01-05T23%3A04%3A22Z&sr=c&si=9350bd2f-ec23-40b2-b27a-248bba01b97e&sig=oGgesnr8mXjCdTM5Dz%2FQpFRBDR0g0%2F60ECoXY14EvsA%3DBigBuckBunny.ism/manifest

它不起作用。

当我直接在浏览器上粘贴此网址时,出现以下错误

AuthenticationFailedServer无法验证请求。确保正确形成Authorization标头的值,包括签名。 RequestId:154422cf-822e-4bbc-af2a-fa69273dfb89时间:2012-12-27T08:57:30.9509847Z签名字段不完整。

但如果我去门户网站发布资产(www.manage.windowsazure.com) - 我喜欢关注protal ..

http://mediaervices.origin.mediaservices.windows.net/5edbeae7-c3e6-45c5-bc5c-70f46b526cb5/BigBuckBunny.ism/Manifest

它适用于我的银色播放器..

现在的问题是我没有收到以代码中的http开头的url,而以https开头的url与我的播放器不兼容。

我猜到了它的安全问题,并试图将我的播放器托管在天蓝色的地方并尝试在那里玩,但没有成功。

1 个答案:

答案 0 :(得分:3)

不,不是安全问题。您正在为Smooth资产请求SAS URL,您需要Origin URL。我的博客上有正确的代码片段: http://blog-ndrouin.azurewebsites.net/?p=1931

具体做法是:

    private static string GetStreamingUrl(CloudMediaContext context, string outputAssetId)
    {
        var daysForWhichStreamingUrlIsActive = 365;

        var outputAsset = context.Assets.Where(a => a.Id == outputAssetId).FirstOrDefault();

        var accessPolicy = context.AccessPolicies.Create(outputAsset.Name, TimeSpan.FromDays(daysForWhichStreamingUrlIsActive), AccessPermissions.Read | AccessPermissions.List);

        var assetFiles = outputAsset.AssetFiles.ToList();

        var assetFile = assetFiles.Where(f => f.Name.ToLower().EndsWith("m3u8-aapl.ism")).FirstOrDefault();
        if (assetFile != null)
        {
            var locator = context.Locators.CreateLocator(LocatorType.OnDemandOrigin, outputAsset, accessPolicy);

            Uri hlsUri = new Uri(locator.Path + assetFile.Name + "/manifest(format=m3u8-aapl)");
            return hlsUri.ToString();
        }

        assetFile = assetFiles.Where(f => f.Name.ToLower().EndsWith(".ism")).FirstOrDefault();
        if (assetFile != null)
        {
            var locator = context.Locators.CreateLocator(LocatorType.OnDemandOrigin, outputAsset, accessPolicy);
            Uri smoothUri = new Uri(locator.Path + assetFile.Name + "/manifest");
            return smoothUri.ToString();
        }

        assetFile = assetFiles.Where(f => f.Name.ToLower().EndsWith(".mp4")).FirstOrDefault();
        if (assetFile != null)
        {
            var locator = context.Locators.CreateLocator(LocatorType.Sas, outputAsset, accessPolicy);
            var mp4Uri = new UriBuilder(locator.Path);
            mp4Uri.Path += "/" + assetFile.Name;
            return mp4Uri.ToString();
        }
        return string.Empty;
    }