OpenTokApi.Core.OpenTok.GenerateToken C# (CSharp) Method

GenerateToken() public method

In order to authenticate a user connecting to a OpenTok session, a user must pass an authentication token along with the API key.
public GenerateToken ( string sessionId, object options = null ) : string
sessionId string /// Optional. An object used to define preferences for the token. /// /// - role : "subscriber", "publisher", "moderator" /// - expire_time : DateTime for when the token should expire /// - connection_data : any metadata you want about the connection limited to 1000 characters ///
options object
return string
        public string GenerateToken(string sessionId, object options = null)
        {
            var paremeters = new RouteValueDictionary(options ?? new object()) {
                {"session_id", sessionId},
                {"create_time",(int) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds},
                {"nonce", _random.Next(0, 999999)}
            };

            if (!paremeters.ContainsKey(TokenProperties.Role))
                paremeters.Add(TokenProperties.Role, Roles.Publisher);

            // Convert expire time to Unix Timestamp
            if (paremeters.ContainsKey(TokenProperties.ExpireTime))
            {
                var origin = new DateTime(1970, 1, 1, 0, 0, 0);
                var expireTime = (DateTime)paremeters[TokenProperties.ExpireTime];
                var diff = expireTime - origin;
                paremeters[TokenProperties.ExpireTime] = Math.Floor(diff.TotalSeconds);
            }

            var data = HttpUtility.ParseQueryString(string.Empty);
            foreach (var pair in paremeters)
                data.Add(CleanupKey(pair.Key), pair.Value.ToString());

            var sig = SignString(data.ToString(), Secret);
            var token = string.Format("{0}{1}", TokenSentinel, EncodeTo64(string.Format("partner_id={0}&sdk_version={1}&sig={2}:{3}", ApiKey, SdkVersion, sig, data)));

            return token;
        }

Usage Example

Esempio n. 1
0
        public openTok(string REMOTE_ADDR)
        {
            OpenTok ot = new OpenTok();

            Dictionary<string, object> options = new Dictionary<string, object>();

              options.Add(SessionProperties.P2PPreference, "enabled");

            this.SessionId = ot.CreateSession(REMOTE_ADDR, options);
            this.Token = ot.GenerateToken(this.SessionId);
            this.ApiKey = ConfigurationManager.AppSettings["opentok.key"];
        }