Skip to main content

Token generators

Authentication is the act of validating the identity of each user before they access a system. In Agora SD-RTN™, this key level of security is implemented in the form of token authentication. Agora SD-RTN™ uses digital tokens to authenticate users and their privileges before they can access Interactive Live Streaming. A token is a dynamic key that is generated from a given set of inputs. All Agora core products are protected using token security.

In order to build a seamless authentication process for your users, you need to integrate token generation into your identity management system. This guide helps you integrate Agora token generation libraries into your authentication system. The integration of Agora token generation and authentication in your security infrastructure provides an additional level of security while allowing you to retain control of the overall authentication process.

Understand the tech

When a user attempts to connect to an Agora channel, your game requests a token from your authentication system. Your authentication system uses Agora libraries to generate a token specific to the channel, then sends it to your game. The game then sends this token to Agora SD-RTN™ with the request to join a channel. Agora SD-RTN™ validates the token and adds the user to the channel. The following diagram shows the authentication call flow between your game, your authentication system and Agora SD-RTN™:

Integrated token generation

Prerequisites

To follow this procedure you must have created:

  • An Agora account and project.

  • A computer with Internet access.

    Ensure that no firewall is blocking your network communication.

Project setup

Open the project in which you wish to integrate token generation.

Integrate token generation into your authentication system

When a user sends an authentication request to your server, the server must check the user's credentials against your internal security and business logic. If everything is in order, the server returns an Agora authentication token to the game. The game then uses this token to join a channel.

This section shows you how to integrate Agora token generation into your authentication system code.

Handle the system logic

To call the token generation methods, you need to add Agora token generation code to your project. To perform this, take the following steps:

  1. Add the Agora IO tools repository

    1. Clone the Agora IO tools repository to your development device:

      git clone https://github.com/AgoraIO/Tools
      Copy
    2. Copy the token generator code to your local project:

      cd Tools/DynamicKey/AgoraDynamicKey/csharp/src/AgoraIO
      cp -r Media/ Common/ Utils/ Extensions/ <Your project path>/
      Copy
  2. Add the NuGet package

    To generate a token, Agora uses the Crc32 algorithm. To add NuGET to your project, open a terminal window in your project folder, then run the following command:

    dotnet add package Crc32.NET
    Copy
  3. Import Agora token builder classes into your project

    Add the following statement at the beginning of the Csharp source file where you wish to generate the Agora token:

    using AgoraIO.Media;
    Copy

Implement token generation

Agora SD-RTN™ supports both integer user Ids and string user accounts for token generation. To ensure smooth communication, all the users in a channel must use the same type of Id, that is, the integer uid. You generate a token using:

  • AccessToken for a token based on uid

To generate tokens based on user ID, add the following token generation code to the source file of your Unity project:

class Program
{
private string appId = "<Your app Id>";
private string appCertificate = "<Your app aertificate>";
private string channelName = "<Your channel name>";
private string uid = "0";
private string userAccount = "User account";
private int expirationTimeInSeconds = 3600; // The time after which the token expires
public void testGenerateDynamicKey()
{
AccessToken token = new AccessToken(appId, appCertificate, channelName, uid);
string token2 = SignalingToken.getToken(appId, appCertificate, userAccount, expirationTimeInSeconds);
// Specify a privilege level and expire time for the token
token.addPrivilege(Privileges.kJoinChannel, Convert.ToUInt32(expirationTimeInSeconds));
string result = token.build();
Console.Write("Token based on uid :");
Console.WriteLine(result);
}
static void Main(string[] args)
{
Program obj = new Program();
obj.testGenerateDynamicKey();
}
}
Copy

Test your implementation

To test the validity of tokens you generate through your integrated authentication system, take the following steps:

  1. In the sample code, set the variables appId, and appCertificate to values from Agora Console.

  2. Set channelName and expirationTimeInSeconds to the name of the channel to join and the renew time for the token.

  3. For the purpose of this test, set uid equal to zero.

  4. Build and run the project

    The code generates two Interactive Live Streaming tokens and prints them to the terminal. The first token is generated using an uid and the second token is generated using a userAccount.

  5. Use the token to connect to a Interactive Live Streaming channel

    1. In your browser, navigate to Agora web demo.
    2. Fill in the App ID, and Channel with the same values that were used to call the token generation method.
    3. Copy the Interactive Live Streaming uid authentication token from the terminal and paste it into the demo page.
    4. Press Join to connect.

You see the local video on the screen.

Connect to the same channel from another device to confirm that everything works.

Reference

This section contains information that completes the information in this page, or points you to documentation that explains other aspects to this product.

You have seen that you generate Agora tokens with a few lines of code. Now integrate the code into your identity management system. Have a look at:

Interactive Live Streaming