// GET: /token
        public IActionResult Index(string Device)
        {
            // Load Twilio configuration from Web.config
            var AccountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
            var ApiKey = Environment.GetEnvironmentVariable("TWILIO_IPM_KEY");
            var ApiSecret = Environment.GetEnvironmentVariable("TWILIO_IPM_SECRET");
            var IpmServiceSid = Environment.GetEnvironmentVariable("TWILIO_IPM_SERVICE_SID");

            // Create a random identity for the client
            var Identity = StringExtensions.MyName.RemoveSpecialCharacters();

            // Create an Access Token generator
            var Token = new AccessToken(AccountSid, ApiKey, ApiSecret);
            Token.Identity = Identity;

            // Create an IP messaging grant for this token
            var grant = new IpMessagingGrant();
            grant.EndpointId = $"QuickStartService:{Identity}:{Device}";            
            grant.ServiceSid = IpmServiceSid;
            Token.AddGrant(grant);

            return Json(new {
                identity = Identity,
                token = Token.ToJWT()
            });
        }
        // 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 ipmServiceSid = ConfigurationManager.AppSettings["TwilioIpmServiceSid"];

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

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

            // Create an IP messaging grant for this token
            var grant = new IpMessagingGrant();
            grant.EndpointId = $"TwilioChatDemo:{identity}:{device}";
            grant.ServiceSid = ipmServiceSid;
            token.AddGrant(grant);

            return Json(new {
                identity = identity,
                token = token.ToJWT()
            }, JsonRequestBehavior.AllowGet);
        }
    static void Main(string[] args)
    {
        // These values are necessary for any access token
        var twilioAccountSid = "ACxxxxxxxxxxxx";
        var twilioApiKey = "SKxxxxxxxxxxxx";
        var twilioApiSecret = "xxxxxxxxxxxxxx";

        // These are specific to IP Messaging
        var ipmServiceSid = "ISxxxxxxxxxxxx";
        var identity = "*****@*****.**";
        var deviceId = "someiosdevice";

        // Create an Access Token generator
        var token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret);
        token.Identity = identity;

        // Create an IP messaging grant for this token
        var grant = new IpMessagingGrant();
        grant.EndpointId = $"HipFlowSlackDockRC:{identity}:{deviceId}";
        grant.ServiceSid = ipmServiceSid;
        token.AddGrant(grant);

        Console.WriteLine(token.ToJWT());
    }
        public string Generate(string identity, string endpointId)
        {
            var token = new AccessToken(
                Configuration.AccountSID, Configuration.ApiKey, Configuration.ApiSecret)
            {
                Identity = identity
            };

            var grant = new IpMessagingGrant {EndpointId = endpointId, ServiceSid = Configuration.IpmServiceSID};
            token.AddGrant(grant);

            return token.ToJWT();
        }