Abhimanyu
Abhimanyu

Reputation: 2213

Increase Azure Media Services SWT Token expiry time

By default in Azure Media Service SWT Token expires after few minutes. That means, video can't be encrypted with that token again. This is great & recommended feature to generate key-token for each playback because keys/token is plain text. But I would like to control this expiry time from code. For the testing purpose I am using Azure Media Explorer Tools suggested on Mingfeiy's blog.

Here is screenshot of this tool which sets end date time and this works great.

enter image description here

Can you please suggest how to achieve this from code? Here is the code I'll be using to set Issuer and Audience information. I guess there will be some way to setup this as well.

    static private string GenerateTokenRequirements()
    {
        Uri _issuer = new Uri(ConfigurationManager.AppSettings["Issuer"]);
        Uri _audience = new Uri(ConfigurationManager.AppSettings["Audience"]);

        TokenRestrictionTemplate template = new TokenRestrictionTemplate(TokenType.SWT);

        template.PrimaryVerificationKey = new SymmetricVerificationKey();
        template.AlternateVerificationKeys.Add(new SymmetricVerificationKey());
        template.Audience = _issuer.ToString();
        template.Issuer = _audience.ToString();

        // Maybe here            

        template.RequiredClaims.Add(TokenClaim.ContentKeyIdentifierClaim);

        return TokenRestrictionTemplateSerializer.Serialize(template);
    }

Thanks in advance.

Upvotes: 0

Views: 257

Answers (1)

Mingfei Yan
Mingfei Yan

Reputation: 645

You could generate token with expiry date with the following method: string testToken = TokenRestrictionTemplateSerializer.GenerateTestToken(tokenTemplate, null, rawkey, DateTime.UtcNow.AddDays(365));

The signature of this method is in this source code: https://github.com/Azure/azure-sdk-for-media-services/blob/07a536cb1b3f5fb19ac0f25c12c7a360794db7d3/src/net/Client/ContentKeyAuthorization/Templates/TokenRestrictionTemplateSerializer.cs

DateTime.UtcNow.AddDays(365) is expiryOn datetime.

Upvotes: 1

Related Questions