string BuildToken(out TimeSpan ttl) { var builder = new SharedAccessSignatureBuilder() { Key = this.SharedAccessKey, TimeToLive = DefaultTokenTimeToLive, }; if (this.SharedAccessKeyName == null) { #if NETMF builder.Target = this.Audience + "/devices/" + WebUtility.UrlEncode(this.DeviceId); #else builder.Target = "{0}/devices/{1}".FormatInvariant(this.Audience, WebUtility.UrlEncode(this.DeviceId)); #endif } else { builder.KeyName = this.SharedAccessKeyName; builder.Target = this.Audience; } ttl = builder.TimeToLive; return(builder.ToSignature()); }
public void DeviceClient_Create_DeviceScope_SharedAccessSignature_Test() { string hostName = "acme.azure-devices.net"; string password = "******"; var sasRule = new SharedAccessSignatureBuilder() { Key = password, Target = hostName + "/devices/" + "device1" }; var authMethod = new DeviceAuthenticationWithToken("device1", sasRule.ToSignature()); var deviceClient = AmqpTransportHandler.Create(hostName, authMethod); Assert.IsNotNull(deviceClient.IotHubConnection); Assert.IsNotNull(((IotHubSingleTokenConnection)deviceClient.IotHubConnection).ConnectionString); }
///<inheritdoc/> protected override Task <string> SafeCreateNewToken(string iotHub, int suggestedTimeToLive) { var builder = new SharedAccessSignatureBuilder { Key = _connectionString.SharedAccessKey, TimeToLive = TimeSpan.FromSeconds(suggestedTimeToLive), }; if (_connectionString.SharedAccessKeyName == null) { builder.Target = "{0}/devices/{1}".FormatInvariant( iotHub, WebUtility.UrlEncode(DeviceId)); } else { builder.KeyName = _connectionString.SharedAccessKeyName; builder.Target = _connectionString.Audience; } return(Task.FromResult(builder.ToSignature())); }
string BuildToken(out TimeSpan ttl) { var builder = new SharedAccessSignatureBuilder() { Key = this.SharedAccessKey, TimeToLive = DefaultTokenTimeToLive, }; if (this.SharedAccessKeyName == null) { builder.Target = "{0}/devices/{1}".FormatInvariant(this.Audience, WebUtility.UrlEncode(this.DeviceId)); } else { builder.KeyName = this.SharedAccessKeyName; builder.Target = this.Audience; } ttl = builder.TimeToLive; return builder.ToSignature(); }