/// <summary> /// This is a helper method to setup a Content Key for delivery from Azure Media Services /// </summary> /// <param name="context">The CloudMediaContext instance used to communicate with Azure Media Services</param> /// <param name="restrictions">One or more restrictions to add to the ContentKeyAuthorizationPolicy associated with the ContentKey</param> /// <returns>A ContentKey that is configured for delivery to clients</returns> private static IContentKey SetupContentKey(CloudMediaContext context, List <ContentKeyAuthorizationPolicyRestriction> restrictions) { // // Create the ContentKeyAuthorizationPolicyOption // IContentKeyAuthorizationPolicyOption option = context.ContentKeyAuthorizationPolicyOptions.Create("Option with Token Restriction", ContentKeyDeliveryType.BaselineHttp, restrictions, null); // // Create the ContentKeyAuthorizationPolicy and add the option from above // IContentKeyAuthorizationPolicy policy = context.ContentKeyAuthorizationPolicies.CreateAsync("Policy to illustrate integration with ACS").Result; policy.Options.Add(option); // // Create the ContentKey and associate the policy from above // byte[] keyValue = GetRandomKeyValue(); Guid keyId = Guid.NewGuid(); IContentKey contentKey = context.ContentKeys.Create(keyId, keyValue, "Test Key", ContentKeyType.EnvelopeEncryption); contentKey.AuthorizationPolicyId = policy.Id; contentKey.Update(); return(contentKey); }
private static void UpdateDeleteContentKey(IContentKey key) { key.AuthorizationPolicyId = Guid.NewGuid().ToString(); key.Update(); key.AuthorizationPolicyId = Guid.NewGuid().ToString(); key.UpdateAsync(); key.Delete(); }
public void QueryContentKeyBYAuthorizationPolicyId() { IContentKeyAuthorizationPolicy policy = _mediaContext.ContentKeyAuthorizationPolicies.CreateAsync(testRun).Result; IContentKey contentKey = _mediaContext.ContentKeys.CreateAsync(Guid.NewGuid(), new byte[16]).Result; contentKey.AuthorizationPolicyId = policy.Id; contentKey.Update(); IContentKey updatedKey = _mediaContext.ContentKeys.Where(c => c.AuthorizationPolicyId == policy.Id).FirstOrDefault(); Assert.IsNotNull(updatedKey.AuthorizationPolicyId); contentKey.Delete(); }
private void SetContentKeyAuthPolicy(IContentKey contentKey, ContentProtection contentProtection) { string policyName = string.Concat(contentKey.Name, Constants.Media.ContentProtection.AuthPolicyName); IContentKeyAuthorizationPolicy authPolicy = GetEntityByName(MediaEntity.ContentKeyAuthPolicy, policyName, true) as IContentKeyAuthorizationPolicy; if (authPolicy == null) { authPolicy = CreateContentKeyAuthPolicy(policyName, contentKey, contentProtection); } if (contentKey.AuthorizationPolicyId != authPolicy.Id) { contentKey.AuthorizationPolicyId = authPolicy.Id; contentKey.Update(); } }
public static async Task CopyDynamicEncryption(IAsset sourceAsset, IAsset destinationAsset, bool RewriteLAURL) { var SourceContext = sourceAsset.GetMediaContext(); var DestinationContext = destinationAsset.GetMediaContext(); // let's copy the keys foreach (var key in sourceAsset.ContentKeys) { IContentKey clonedkey = DestinationContext.ContentKeys.Where(k => k.Id == key.Id).FirstOrDefault(); if (clonedkey == null) // key does not exist in target account { try { clonedkey = DestinationContext.ContentKeys.Create(new Guid(key.Id.Replace(Constants.ContentKeyIdPrefix, "")), key.GetClearKeyValue(), key.Name, key.ContentKeyType); } catch { // we cannot create the key but the guid is taken. throw new Exception(String.Format("Key {0} is not in the account but it cannot be created (same key id already exists in the datacenter? ", key.Id)); } } destinationAsset.ContentKeys.Add(clonedkey); if (key.AuthorizationPolicyId != null) { IContentKeyAuthorizationPolicy sourcepolicy = SourceContext.ContentKeyAuthorizationPolicies.Where(ap => ap.Id == key.AuthorizationPolicyId).FirstOrDefault(); if (sourcepolicy != null) // there is one { IContentKeyAuthorizationPolicy clonedpolicy = (clonedkey.AuthorizationPolicyId != null) ? DestinationContext.ContentKeyAuthorizationPolicies.Where(ap => ap.Id == clonedkey.AuthorizationPolicyId).FirstOrDefault() : null; if (clonedpolicy == null) { clonedpolicy = await DestinationContext.ContentKeyAuthorizationPolicies.CreateAsync(sourcepolicy.Name); foreach (var opt in sourcepolicy.Options) { IContentKeyAuthorizationPolicyOption policyOption = DestinationContext.ContentKeyAuthorizationPolicyOptions.Create(opt.Name, opt.KeyDeliveryType, opt.Restrictions, opt.KeyDeliveryConfiguration); clonedpolicy.Options.Add(policyOption); } clonedpolicy.Update(); } clonedkey.AuthorizationPolicyId = clonedpolicy.Id; } } clonedkey.Update(); } //let's copy the policies foreach (var delpol in sourceAsset.DeliveryPolicies) { Dictionary <AssetDeliveryPolicyConfigurationKey, string> assetDeliveryPolicyConfiguration = new Dictionary <AssetDeliveryPolicyConfigurationKey, string>(); foreach (var s in delpol.AssetDeliveryConfiguration) { string val = s.Value; string ff = AssetDeliveryPolicyConfigurationKey.PlayReadyLicenseAcquisitionUrl.ToString(); if (RewriteLAURL && (s.Key.ToString().Equals(AssetDeliveryPolicyConfigurationKey.PlayReadyLicenseAcquisitionUrl.ToString()) || s.Key.ToString().Equals(AssetDeliveryPolicyConfigurationKey.EnvelopeKeyAcquisitionUrl.ToString()) )) { // let's change the LA URL to use the account in the other datacenter val = val.Replace(SourceContext.Credentials.ClientId, DestinationContext.Credentials.ClientId); } assetDeliveryPolicyConfiguration.Add(s.Key, val); } var clonetargetpolicy = DestinationContext.AssetDeliveryPolicies.Create(delpol.Name, delpol.AssetDeliveryPolicyType, delpol.AssetDeliveryProtocol, assetDeliveryPolicyConfiguration); destinationAsset.DeliveryPolicies.Add(clonetargetpolicy); } }
static IAsset CreateAssetAndProtectedStreamingLocatorInV2(CloudMediaContext v2Client, ConfigWrapper config, string assetNameOrDescription) { // Create the input Asset IAsset originalAsset = v2Client.Assets.Create("input asset", AssetCreationOptions.None); string filename = Path.GetFileName(config.FilePathToUpload); IAssetFile assetFile = originalAsset.AssetFiles.Create(filename); assetFile.Upload(config.FilePathToUpload); // Submit a job to encode the single input file into an adaptive streaming set IJob job = v2Client.Jobs.Create("Media Encoder Standard Job"); IMediaProcessor processor = GetMediaEncoderStandardProcessor(v2Client); ITask task = job.Tasks.AddNew("Adaptive Streaming encode", processor, "Adaptive Streaming", TaskOptions.None); task.InputAssets.Add(originalAsset); task.OutputAssets.AddNew(assetNameOrDescription, AssetCreationOptions.None); job.Submit(); job.GetExecutionProgressTask(CancellationToken.None).Wait(); // Get the output asset to publish job.Refresh(); IAsset assetToPublish = v2Client.Assets.Where(a => a.Id == job.Tasks[0].OutputAssets[0].Id).First(); // Create the content key Guid keyId = Guid.NewGuid(); byte[] contentKey = GetRandomBuffer(16); IContentKey key = v2Client.ContentKeys.Create(keyId, contentKey, "ContentKey", ContentKeyType.EnvelopeEncryption); // Create ContentKeyAuthorizationPolicy with Open restriction and create authorization policy IContentKeyAuthorizationPolicy policy = v2Client.ContentKeyAuthorizationPolicies.CreateAsync("Open Authorization Policy").Result; ContentKeyAuthorizationPolicyRestriction restriction = new ContentKeyAuthorizationPolicyRestriction { Name = "Open Authorization Policy", KeyRestrictionType = (int)ContentKeyRestrictionType.Open, Requirements = null }; List <ContentKeyAuthorizationPolicyRestriction> restrictions = new List <ContentKeyAuthorizationPolicyRestriction>(); restrictions.Add(restriction); var policyOption = v2Client.ContentKeyAuthorizationPolicyOptions.Create("policy", ContentKeyDeliveryType.BaselineHttp, restrictions, ""); policy.Options.Add(policyOption); // Add ContentKeyAuthorizationPolicy to ContentKey key.AuthorizationPolicyId = policy.Id; key.Update(); assetToPublish.ContentKeys.Add(key); Uri keyAcquisitionUri = key.GetKeyDeliveryUrl(ContentKeyDeliveryType.BaselineHttp); UriBuilder uriBuilder = new UriBuilder(keyAcquisitionUri); uriBuilder.Query = String.Empty; keyAcquisitionUri = uriBuilder.Uri; // The following policy configuration specifies: // key url that will have KID=<Guid> appended to the envelope and // the Initialization Vector (IV) to use for the envelope encryption. var assetDeliveryPolicyConfiguration = new Dictionary <AssetDeliveryPolicyConfigurationKey, string> { { AssetDeliveryPolicyConfigurationKey.EnvelopeBaseKeyAcquisitionUrl, keyAcquisitionUri.ToString() }, }; var assetDeliveryPolicy = v2Client.AssetDeliveryPolicies.Create("AssetDeliveryPolicy", AssetDeliveryPolicyType.DynamicEnvelopeEncryption, AssetDeliveryProtocol.SmoothStreaming | AssetDeliveryProtocol.HLS | AssetDeliveryProtocol.Dash, assetDeliveryPolicyConfiguration); // Add AssetDelivery Policy to the asset assetToPublish.DeliveryPolicies.Add(assetDeliveryPolicy); // Create a 30-day readonly access policy. // You cannot create a streaming locator using an AccessPolicy that includes write or delete permissions. IAccessPolicy accessPolicy = v2Client.AccessPolicies.Create("Streaming Access Policy", TimeSpan.FromDays(365 * 100), AccessPermissions.Read); // Create a locator to the streaming content on an origin. ILocator originLocator = v2Client.Locators.CreateLocator(LocatorType.OnDemandOrigin, assetToPublish, accessPolicy, DateTime.UtcNow.AddMinutes(-5)); // remove the original input asset as we don't need it for demonstration purposes originalAsset.Delete(); return(assetToPublish); }