// GET: /token
        public ActionResult Index(string device)
        {
            // Load Twilio configuration from Web.config
            var accountSid     = ConfigurationManager.AppSettings["TwilioAccountSid"];
            var apiKey         = ConfigurationManager.AppSettings["TwilioApiKey"];
            var apiSecret      = ConfigurationManager.AppSettings["TwilioApiSecret"];
            var syncServiceSid = ConfigurationManager.AppSettings["TwilioSyncServiceSid"];

            // Create a random identity for the client
            var identity = Internet.UserName();

            // Create an Access Token generator
            var token = new AccessToken(accountSid, apiKey, apiSecret)
            {
                Identity = identity
            };

            // Create a Sync grant for this token
            var grant = new SyncGrant
            {
                EndpointId = $"TwilioSyncQuickstart:{identity}:{device}",
                ServiceSid = syncServiceSid
            };

            token.AddGrant(grant);

            return(Json(new
            {
                identity,
                token = token.ToJWT()
            }, JsonRequestBehavior.AllowGet));
        }
        public JsonResult Token()
        {
            // This can be tracked internally by your web app.
            var identity = randomUserId();


            var grants = new HashSet <IGrant>();

            var videoGrant = new VideoGrant();

            grants.Add(videoGrant);

            if (_appSettings.TWILIO_CHAT_SERVICE_SID != String.Empty)
            {
                // Create a "grant" which enables a client to use IPM as a given user,
                // on a given device.
                var ipmGrant = new IpMessagingGrant()
                {
                    ServiceSid = _appSettings.TWILIO_CHAT_SERVICE_SID,
                };

                grants.Add(ipmGrant);
            }

            if (_appSettings.TWILIO_SYNC_SERVICE_SID != String.Empty)
            {
                var syncGrant = new SyncGrant()
                {
                    ServiceSid = _appSettings.TWILIO_SYNC_SERVICE_SID
                };

                grants.Add(syncGrant);
            }

            var token = new Token(
                _appSettings.TWILIO_ACCOUNT_SID,
                _appSettings.TWILIO_API_KEY,
                _appSettings.TWILIO_API_SECRET,
                identity,
                grants: grants
                ).ToJwt();

            return(new JsonResult(new Dictionary <string, string>()
            {
                { "identity", identity },
                { "token", token }
            }));
        }
        private JsonResult CreateTokenResult(string identity)
        {
            var grants = new HashSet <IGrant>();

            var videoGrant = new VideoGrant();

            grants.Add(videoGrant);

            if (_twilioAccount.ChatServiceSid != String.Empty)
            {
                // Create a "grant" which enables a client to use IPM as a given user,
                // on a given device.
                var chatGrant = new ChatGrant()
                {
                    ServiceSid = _twilioAccount.ChatServiceSid
                };

                grants.Add(chatGrant);
            }

            if (_twilioAccount.SyncServiceSid != String.Empty)
            {
                var syncGrant = new SyncGrant()
                {
                    ServiceSid = _twilioAccount.SyncServiceSid
                };

                grants.Add(syncGrant);
            }

            var token = new Token(

                _twilioAccount.AccountSid,
                _twilioAccount.ApiKey,
                _twilioAccount.ApiSecret,
                identity,
                grants: grants
                ).ToJwt();

            return(new JsonResult(new Dictionary <string, string>()
            {
                { "identity", identity },
                { "token", token }
            }));
        }
示例#4
0
        public Task <string> Generate(string identity)
        {
            var grants = new HashSet <IGrant>();

            var videoGrant = new VideoGrant();

            grants.Add(videoGrant);

            if (_account.ChatServiceSid != string.Empty)
            {
                var chatGrant = new ChatGrant()
                {
                    ServiceSid = _account.ChatServiceSid,
                };

                grants.Add(chatGrant);
            }

            if (_account.SyncServiceSid != string.Empty)
            {
                var syncGrant = new SyncGrant()
                {
                    ServiceSid = _account.SyncServiceSid
                };

                grants.Add(syncGrant);
            }

            var token = new Token(
                _account.AccountSid,
                _account.ApiKey,
                _account.ApiSecret,
                identity,
                grants: grants
                ).ToJwt();

            return(Task.FromResult(token));
        }