FabRueil
FabRueil

Reputation: 1

Pass a token via the addition of a URL query parameter with "token=tokenvalue." in Azure Media Service not working

Trying to stream a video from Azure Media Service into a Xamarin.Form application. The asset is protected with a JWT token.

I use the following code to generate the token:

private string GetJWT(string PrimaryKey) {

 var tokenSigningKey = new SymmetricSecurityKey(Convert.FromBase64String(PrimaryKey));

 SigningCredentials cred = new SigningCredentials(tokenSigningKey, SecurityAlgorithms.HmacSha256);

 JwtSecurityToken token = new JwtSecurityToken(
     issuer: "xxx",
     audience: "yyy",
     claims: null,
     notBefore: DateTime.Now.AddMinutes(-5),
     expires: DateTime.Now.AddMinutes(60),
     signingCredentials: cred);

     JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();

     return handler.WriteToken(token);
}

The URI for the asset is like : https://xxx.streaming.media.azure.net/12222-1565-232323-a5b8-c10e148273ae/Test.ism/manifest(format=m3u8-cmaf,encryption=cbc)

If I use Azure Media Player (https://ampdemo.azureedge.net) to test the URI and the AES token, it works fine. So I guess there is no problem with the token itself...

The documentation (https://learn.microsoft.com/en-us/azure/media-services/latest/security-pass-authentication-tokens-how-to#pass-a-token-via-the-addition-of-a-url-query-parameter-with-tokentokenvalue) says that the following code should work to send the token directly with the url. I need to do this as with Xamarin.Forms and MediaElement, I can't send the token in a header request. So I need the querystring option...

string armoredAuthToken = System.Web.HttpUtility.UrlEncode(authToken);
string uriWithTokenParameter = string.Format("{0}&token={1}", keyDeliveryServiceUri.AbsoluteUri, armoredAuthToken);
Uri keyDeliveryUrlWithTokenParameter = new Uri(uriWithTokenParameter);

player.Source = keyDeliveryUrlWithTokenParameter;   (player is a MediaElement control)

But the video is never loaded. In my opinion there is an error, it should be {0}?token={1} instead of {0}&token={1}. But that doesn't work neither.

If I test with VLC the https://xxx.streaming.media.azure.net/12222-1565-232323-a5b8-c10e148273ae/Test.ism/manifest(format=m3u8-cmaf,encryption=cbc)?token=zzzzz, it doesn't work neither.

I presume there is a problem with token in the querystring, as if Azure can't read it.

Upvotes: 0

Views: 184

Answers (1)

keni
keni

Reputation: 1768

Probably late, and FWIW, since Azure Media Services is going down in mid 2024 anyway... 2 issues:

  1. You should be appending the token=token-value to the end of the licenseUrl, NOT to the end of the stream URL.
  2. Still related, {0}&token={1} could have given you pointers to the problem. The license URL already includes a ?. For example, a licenseURL (which you can find if you look at the response of doing an HTTP GET to your streaming URL ending with manifest(format=m3u8-cmaf,encryption=cbc)) looks like this:

https://xxx.keydelivery.westeurope.media.azure.net/Widevine/?kid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Upvotes: 0

Related Questions