コード例 #1
0
        public async Task<Task> StartAsync(string deviceId, CancellationToken cancellationToken)
        {
            string deviceSas =
                new SharedAccessSignatureBuilder
                {
                    Key = this.deviceKey,
                    Target = $"{this.connectionStringBuilder.HostName}/devices/{deviceId}",
                    TimeToLive = TimeSpan.FromDays(3)
                }
                    .ToSignature();

            var taskCompletionSource = new TaskCompletionSource();
            Bootstrap b = this.bootstrap.Clone();
            var readHandler = new ReadListeningHandler(CommunicationTimeout);
            b.Handler(new ActionChannelInitializer<ISocketChannel>(
                ch =>
                {
                    ch.Pipeline.AddLast(
                        new TlsHandler(stream => new SslStream(stream, true, (sender, certificate, chain, errors) => true), new ClientTlsSettings(this.tlsHostName)),
                        MqttEncoder.Instance,
                        new MqttDecoder(false, 256 * 1024),
                        readHandler,
                        ConsoleLoggingHandler.Instance);
                }));

            var channel = await b.ConnectAsync(this.endpoint);

            await this.GetCompleteScenarioAsync(channel, readHandler, deviceId, deviceSas, cancellationToken);
            
            return taskCompletionSource.Task;
        }
コード例 #2
0
        public async Task<Task> StartAsync(string deviceId, CancellationToken cancellationToken)
        {
            string deviceSas =
                new SharedAccessSignatureBuilder
                {
                    Key = this.deviceKey,
                    Target = string.Format("{0}/devices/{1}", this.connectionStringBuilder.HostName, deviceId),
                    TimeToLive = TimeSpan.FromDays(3)
                }
                    .ToSignature();

            var taskCompletionSource = new TaskCompletionSource();
            var b = (Bootstrap)this.bootstrap.Clone();
            b.Handler(new ActionChannelInitializer<ISocketChannel>(
                ch =>
                {
                    ch.Pipeline.AddLast(
                        TlsHandler.Client(this.tlsHostName, null, (sender, certificate, chain, errors) => true),
                        MqttEncoder.Instance,
                        new MqttDecoder(false, 256 * 1024),
                        new TestScenarioRunner(
                            cmf => this.GetCompleteScenario(cmf, deviceId, deviceSas, cancellationToken),
                            taskCompletionSource,
                            CommunicationTimeout,
                            CommunicationTimeout),
                        ConsoleLoggingHandler.Instance);
                }));

            await b.ConnectAsync(this.endpoint);

            return taskCompletionSource.Task;
        }
コード例 #3
0
ファイル: MainForm.cs プロジェクト: neo7206/azure-iot-sdks
        private void generateSASButton_Click(object sender, EventArgs e)
        {
            try
            {
                var builder = new SharedAccessSignatureBuilder()
                {
                    KeyName = keyNameTextBox.Text,
                    Key = keyValueTextBox.Text,
                    Target = targetTextBox.Text,
                    TimeToLive = TimeSpan.FromDays(Convert.ToDouble(numericUpDownTTL.Value))
                }.ToSignature();

                sasRichTextBox.Text = builder + "\r\n";
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Unable to generate SAS. Verify connection strings.\n{0}", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #4
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);
                }
            }

        }
コード例 #5
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();
        }
コード例 #6
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);
                }
            }

        }