コード例 #1
0
        public void ConfigureMqttListener()
        {
            string address       = $"{this.IoTHubNamespace}.azure-devices.net";
            string username      = $"{address}/{this.DeviceId}/?api-version=2018-06-30";
            string resourceUrl   = $"{address}/devices/{this.DeviceId}";
            string password      = SharedAccessSignatureBuilder.GetSASToken(username, this.DeviceKey, null, 24);
            string testerAddress = "net.pipe://localhost/AzureIoTHubTester_" + Process.GetCurrentProcess().Id;

            string     id     = Guid.NewGuid().ToString();
            ConfigData config = new ConfigData()
            {
                Name = this.DeviceId, Id = id, Username = username, Password = password, BrokerPort = this.Port, BrokerAddress = address, TesterAddress = testerAddress
            };

            string appDomainName = config.Name + "/" + config.Id;

            config.HostName = appDomainName;

            string contentTypeAndEncoding = $"$.ct=application%2Fjson&$.ce=utf-8";

            ThreadPool.QueueUserWorkItem(delegate(object state)
            {
                try
                {
                    HostServices.Current.Add(appDomainName, config);
                    HostServices.Current.Open(appDomainName);
                }
                catch (Exception ex)
                {
                    HostServices.Current.Close(appDomainName);
                }
            });
        }
コード例 #2
0
        public static string GetSASToken(string resourceUri, string key, string keyName = null, uint hours = 24)
        {
            var    expiry       = GetExpiry(hours);
            string stringToSign = System.Web.HttpUtility.UrlEncode(resourceUri) + "\n" + expiry;
            var    signature    = SharedAccessSignatureBuilder.ComputeSignature(key, stringToSign);
            var    sasToken     = keyName == null?
                                  String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}", HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry) :
                                      String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName);

            return(sasToken);
        }