C#C# V4 Token Management System API Reference for Realtime Apps

The Token Management System (TMS) is a part of the C# SDK which allows you to store access tokens obtained via GrantToken(). If a particular PubNub method requires authentication, the TMS will perform a lookup in order to select an apropriate access token, and attach it to the request in question - as the authKey query parameter.

 

Currently, the TMS selects tokens only for requests from the Objects API. For authorizing other requests, please use the classic Grant() method.

The TMS is automatically disabled if the PubNub instance it runs on has a valid secret key, meaning although you can set tokens into the TMS, it will not authorize requests automatically.


Otherwise, the TMS is enabled by default, but can also be disabled explicitly via pnConfiguration.EnableTokenManager = false;.

Stores a single token in the Token Management System for use in API calls.

  1. ParameterTypeRequiredDescription
    tokenStringYesThe token to add to the Token Management System.
/*
* A token is usually not hardcoded.
*
* Tokens are returned as a result to calling 'GrantToken()' on a PubNub
* server instance.
*
* Use that token as the parameter to 'pubnub.SetToken()' on a PubNub
* client instance.
*/

pubnub.SetToken("p0F2AkF0Gl2AX-JDdHRsCkNyZXOkRGNoYW6cKBDdXNyoWl1LTMzNTIwNTUPQ3NwY6...");

The SetToken() method does not return anything.

Stores multiple tokens in the Token Management System for use in API calls.

  1. ParameterTypeRequiredDescription
    tokensstring[]YesThe array of tokens to add to the Token Management System.
pubnub.SetTokens(new string[] {"p0F2AkF0Gl2AX-JDdHRsCkNyZXOk...", "RGNoYW6gQ2dycKBDNTIwNTUPQ3NwY6..." });

The SetTokens() method does not return anything.

Perfoms a lookup and returns a token for the specified resource type and resource Id. If no token is found for the supplied resource type and resource Id, the TMS checks for a pattern token at the resource type level. Returns null if no token is found.

  1. ParameterTypeRequiredDescription
    resourceIdstringYesThe resource ID for which the token is to be retrieved.
    resourceTypestringYesThe resource type of the resource for which the token is to be retrieved.
string res = pubnub.GetToken("space", "testspace_15011");
Console.WriteLine(res)

The GetToken() operation returns a string which contains the token.

Returns a list of all tokens stored by SetToken/SetTokens, or stored when tokens are granted via GrantToken calls.

Dictionary<PNTokenKey, string> tokensDic = pubnub.GetTokens();
if (tokensDic != null && tokensDic.Count > 0)
{
    foreach (KeyValuePair<PNTokenKey, string> kvp in tokensDic)
    {
        PNTokenKey key = kvp.Key;
        Console.WriteLine(string.Format("ResourceType: {0}; ResourceId: {1}; IsPattern: {2}; Token: {3}", key.ResourceType, key.ResourceId, Convert.ToBoolean(key.PatternFlag), kvp.Value));
    }
}

The GetTokens() method returns a Dictionary<PNTokenKey, string>, where the key is of type PNTokenKey and the value being the actual access token.

PropertyTypeDescription
ResourceTypestringThe resource type of the token, either user or space.
ResourceIdstringThe resource id of the token
PatternFlagintIndicator whether the resource id is pattern or not.

Returns the token(s) for the specified resource type.

  1. ParameterTypeRequiredDescription
    resourceTypestringYesThe resource type of the resource for which the token is to be retrieved.

Get Tokens By Resource Type

Dictionary<PNTokenKey, string> tokensDic = pubnub.GetTokensByResource("user");
if (tokensDic != null && tokensDic.Count > 0)
{
    foreach (KeyValuePair<PNTokenKey, string> kvp in tokensDic)
    {
        PNTokenKey key = kvp.Key;
        Console.WriteLine(string.Format("ResourceType: {0}; ResourceId: {1}; IsPattern: {2}; Token: {3}", key.ResourceType, key.ResourceId, Convert.ToBoolean(key.PatternFlag), kvp.Value));
    }
}

The GetTokensByResource() method returns a Dictionary<PNTokenKey, string>, where the key is of type PNTokenKey and the value being the actual access token.

PropertyTypeDescription
ResourceTypestringThe resource type of the token, either user or space.
ResourceIdstringThe resource id of the token.
PatternFlagintIndicator whether the resource id is pattern or not.