コード例 #1
0
        string BuildToken(out TimeSpan ttl)
        {
            var builder = new SharedAccessSignatureBuilder()
            {
                KeyName = this.SharedAccessKeyName,
                Key = this.SharedAccessKey,
                TimeToLive = DefaultTokenTimeToLive,
                Target = this.Audience
            };

            ttl = builder.TimeToLive;

            return builder.ToSignature();
        }
コード例 #2
0
        private void generateButton_Click(object sender, EventArgs e)
        {
            try
            {
                decimal ttlValue = numericUpDown1.Value;

                var sasBuilder = new SharedAccessSignatureBuilder()
                {
                    Key = this.selectedDeviceKey,
                    Target = String.Format("{0}/devices/{1}", iotHubHostName, WebUtility.UrlEncode(this.selectedDeviceId)),
                    TimeToLive = TimeSpan.FromDays(Convert.ToDouble(ttlValue))
                };
                
                sasRichTextBox.Text = deviceConnectionStringWithSAS(sasBuilder.ToSignature()) + "\r\n";

            }
            catch (Exception ex)
            {
                using (new CenterDialog(this))
                {
                    MessageBox.Show($"Unable to generate SAS. Verify connection strings.\n{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

        }
コード例 #3
0
        private void generateButton_Click(object sender, EventArgs e)
        {
            try
            {
                var selectedDevice = devicesDictionary[this.selectedDeviceId];

                if (selectedDevice.Authentication.X509Thumbprint != null)
                {
                    if ( (selectedDevice.Authentication.X509Thumbprint.PrimaryThumbprint != null) ||
                         (selectedDevice.Authentication.X509Thumbprint.SecondaryThumbprint != null) )
                    {
                        throw new Exception("Cannot generate SAS token for device with X509 Authentication!");
                    }
                }

                decimal ttlValue = numericUpDown1.Value;

                var sasBuilder = new SharedAccessSignatureBuilder()
                {
                    Key = this.selectedDeviceKey,
                    Target = String.Format("{0}/devices/{1}", iotHubHostName, WebUtility.UrlEncode(this.selectedDeviceId)),
                    TimeToLive = TimeSpan.FromDays(Convert.ToDouble(ttlValue))
                };
                
                sasRichTextBox.Text = deviceConnectionStringWithSAS(sasBuilder.ToSignature()) + "\r\n";

            }
            catch (Exception ex)
            {
                using (new CenterDialog(this))
                {
                    MessageBox.Show($"Unable to generate SAS.\n{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

        }