Suggestions

close search

Add Messaging, Voice, and Authentication to your apps with Vonage Communications APIs

Visit the Vonage API Developer Portal
Developer Documentation moved on August 7, 2026. Future updates are available only at → https://developer.vonage.com/en/opentok/overview

Connection Token Creation — .NET

In order to authenticate a user connecting to an OpenTok session, a client must connect using a token (see this overview).

The following .NET code shows how to generate a token using the OpenTok .NET SDK:

// Set the following constants with the API key and API secret
// that you receive when you sign up to use the OpenTok API:
OpenTok opentok = new OpenTok(API_KEY, API_SECRET);

//Generate a basic session. Or you could use an existing session ID.
string sessionId = opentok.CreateSession().Id

string token = opentok.GenerateToken(sessionId);

Calling the GenerateToken() method returns a string. This string is the token.

The following .NET code shows how to obtain a token that has a role of "publisher" and that has a connection metadata string:

// Set the following constants with the API key and API secret
// that you receive when you sign up to use the OpenTok API:
OpenTok opentok = new OpenTok(API_KEY, API_SECRET);

//Generate a basic session. Or you could use an existing session ID.
string sessionId = opentok.CreateSession().Id

// Replace with meaningful metadata for the connection.
string connectionMetadata = "username=Bob,userLevel=4";

// Generate a token. Use the Role value appropriate for the user.
string token = opentok.GenerateToken(sessionId, Role.PUBLISHER, Null, connectionMetadata);
=

The method takes the following arguments: