Search code examples
c#agora.ioagora-web-sdk-ng

Web Agora.io DYNAMIC_KEY_EXPIRED


I am using c# with mvc.

I used this code to generate token and generated successfully. but after generate token when join channel using .join() it return DYNAMIC_KEY_EXPIRED. I Used "AgoraRTCSDK-3.1.0.js"

I used https://github.com/AgoraIO/Tools/blob/master/DynamicKey/AgoraDynamicKey/csharp to generate dynamic token

If any one has experience on Agora.io, please help me.

Sample code is..

AccessToken token = new AccessToken(apiKey, appCertificate, channelName, "0");
token.addPrivilege(Privileges.kJoinChannel, _expiredTs);
token.addPrivilege(Privileges.kPublishAudioStream, _expiredTs);
token.addPrivilege(Privileges.kPublishVideoStream, _expiredTs);
string strToken = token.build();

public string build()
    {
        this._messageRawContent = Utils.pack(this.message);
        this._signature = generateSignature(_appCertificate
                , _appId
                , _channelName
                , _uid
                , _messageRawContent);

        this._crcChannelName = Crc32CAlgorithm.Compute(this._channelName.GetByteArray());
        this._crcUid = Crc32CAlgorithm.Compute(this._uid.GetByteArray());

        PackContent packContent = new PackContent(_signature, _crcChannelName, _crcUid, this._messageRawContent);
        byte[] content = Utils.pack(packContent);
        return getVersion() + this._appId + Utils.base64Encode(content);
    }

Solution

  • Whenever you generate a token for Agora applications, you need to keep in mind that the expiration time is calculated as a timestamp (time since 1970) so you need to ensure the the expiration time is set to the currentTime + expirationTimeInSeconds.

    In the above example you are passing the expiration time as 0 which generates a token that is already considered expired.

    consider using:

      // set a expiration time of 1 hour in seconds
      let expireTime = 3600;
    
      // calculate current time in seconds
      const currentTime = Math.floor(Date.now() / 1000);
    
      // calculate privilege expire time
      const privilegeExpireTime = currentTime + expireTime;