private IotHubServiceClient(IotHubSasCredential credential, IotHubServiceClientOptions options) { options ??= new IotHubServiceClientOptions(); _clientDiagnostics = new ClientDiagnostics(options); options.AddPolicy(new BearerTokenAuthenticationPolicy(credential, s_authorizationScopes), HttpPipelinePosition.PerCall); _httpPipeline = HttpPipelineBuilder.Build(options); _devicesRestClient = new DevicesRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, options.GetVersionString()); _modulesRestClient = new ModulesRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, options.GetVersionString()); _queryRestClient = new QueryRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, options.GetVersionString()); _statisticsRestClient = new StatisticsRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, options.GetVersionString()); _configurationRestClient = new ConfigurationRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, options.GetVersionString()); // Note that the devices and modules subclient take a reference to the Query convenience layer client. This // is because they each expose a helper function that uses the query client for listing twins. By passing in // the convenience layer query client rather than the protocol layer query client, we minimize rewriting the // same pagination logic that now exists only in the query convenience layer client. Query = new QueryClient(_queryRestClient); Devices = new DevicesClient(_devicesRestClient, Query); Modules = new ModulesClient(_devicesRestClient, _modulesRestClient, Query); Statistics = new StatisticsClient(_statisticsRestClient); Configurations = new ConfigurationsClient(_configurationRestClient); Messages = new CloudToDeviceMessagesClient(); Files = new FilesClient(); Jobs = new JobsClient(); }
public void CtorSetsDefaultTimeToLive() { // Arrange string sharedAccessPolicy = "policy"; string sharedAccessKey = "key"; // Act var credentials = new IotHubSasCredential(sharedAccessPolicy, sharedAccessKey); // Assert credentials.SasTokenTimeToLive.Should().BePositive(); }
/// <summary> /// Main entry point to the sample. /// </summary> public static async Task Main(string[] args) { // Parse and validate parameters CommandLineOptions options = null; ParserResult <CommandLineOptions> result = Parser.Default.ParseArguments <CommandLineOptions>(args) .WithParsed(parsedOptions => { options = parsedOptions; }) .WithNotParsed(errors => { Environment.Exit(1); }); // Instantiate the client #region Snippet:IotHubServiceClientInitializeWithIotHubSasCredential // Create an IotHubSasCredential type to use sas tokens to authenticate against your IoT Hub instance. // The default lifespan of the sas token is 30 minutes, and it is set to be renewed when at 15% or less of its lifespan. var credential = new IotHubSasCredential(options.IotHubSharedAccessPolicy, options.IotHubSharedAccessKey); IotHubServiceClient hubClient = new IotHubServiceClient(options.Endpoint, credential); #endregion Snippet:IotHubServiceClientInitializeWithIotHubSasCredential // Run the samples var deviceIdentityLifecycleSamples = new DeviceIdentityLifecycleSamples(hubClient); await deviceIdentityLifecycleSamples.RunSampleAsync(); var moduleIdentityLifecycleSamples = new ModuleIdentityLifecycleSamples(hubClient); await moduleIdentityLifecycleSamples.RunSampleAsync(); var bulkDeviceIdentityLifecycleSamples = new BulkDeviceIdentityLifecycleSamples(hubClient); await bulkDeviceIdentityLifecycleSamples.RunSampleAsync(); var bulkModuledentityLifecycleSamples = new BulkModuleIdentityLifecycleSamples(hubClient); await bulkModuledentityLifecycleSamples.RunSampleAsync(); var querySamples = new QueryTwinSamples(hubClient); await querySamples.RunSampleAsync(); // Run samples that require the device sample to be running. if (options.IsDeviceSampleRunning == true) { // This sample requires the device sample to be running so that it can connect to the device. var methodInvocationSamples = new MethodInvocationSamples(hubClient); await methodInvocationSamples.RunSampleAsync(); } }
public async Task RegeneratesTokenIfExpired() { // Arrange var ttl = TimeSpan.FromSeconds(1); var ctx = new TokenRequestContext(); var cts = new CancellationTokenSource(); // Act var credential = new IotHubSasCredential("policy", s_testSharedAccessKey, ttl) { Endpoint = s_endpoint, }; string initialToken = credential.GetToken(ctx, cts.Token).Token; await Task.Delay(2000).ConfigureAwait(false); string newToken = credential.GetToken(ctx, cts.Token).Token; // Assert newToken.Should().NotBeEquivalentTo(initialToken, "Token has expired, and should be regenerated"); }
public async Task CachesTokenIfNotExpired() { // Arrange var ttl = TimeSpan.FromMinutes(1); var ctx = new TokenRequestContext(); var cts = new CancellationTokenSource(); // Act var credential = new IotHubSasCredential("policy", s_testSharedAccessKey, ttl) { Endpoint = s_endpoint, }; string initialToken = credential.GetToken(ctx, cts.Token).Token; await Task.Delay(2000); string newToken = credential.GetToken(ctx, cts.Token).Token; // Assert newToken.Should().BeEquivalentTo(initialToken, "Token should be cached and returned"); }
private IotHubServiceClient(IotHubSasCredential credential, IotHubServiceClientOptions options) { options ??= new IotHubServiceClientOptions(); _clientDiagnostics = new ClientDiagnostics(options); options.AddPolicy(new SasTokenAuthenticationPolicy(credential), HttpPipelinePosition.PerCall); _httpPipeline = HttpPipelineBuilder.Build(options); _devicesRestClient = new DevicesRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, options.GetVersionString()); _modulesRestClient = new ModulesRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, options.GetVersionString()); _queryRestClient = new QueryRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, options.GetVersionString()); _statisticsRestClient = new StatisticsRestClient(_clientDiagnostics, _httpPipeline, credential.Endpoint, options.GetVersionString()); Devices = new DevicesClient(_devicesRestClient, _queryRestClient); Modules = new ModulesClient(_devicesRestClient, _modulesRestClient, _queryRestClient); Statistics = new StatisticsClient(_statisticsRestClient); Messages = new CloudToDeviceMessagesClient(); Files = new FilesClient(); Jobs = new JobsClient(); }
private static IotHubSasCredential SetEndpointToIotHubSasCredential(Uri endpoint, IotHubSasCredential credential) { credential.Endpoint = endpoint; return(credential); }
/// <summary> /// Initializes a new instance of the <see cref="IotHubServiceClient"/> class. /// </summary> /// <param name="endpoint"> /// The IoT Hub service instance endpoint to connect to. /// </param> /// <param name="credential"> /// The IoT Hub credentials, to be used for authenticating against an IoT Hub instance via SAS tokens. /// </param> /// <param name="options"> /// (optional) Options that allow configuration of requests sent to the IoT Hub service. /// </param> /// <code snippet="Snippet:IotHubServiceClientInitializeWithIotHubSasCredential"> /// // Create an IotHubSasCredential type to use sas tokens to authenticate against your IoT Hub instance. /// // The default lifespan of the sas token is 30 minutes, and it is set to be renewed when at 15% or less of its lifespan. /// var credential = new IotHubSasCredential(options.IotHubSharedAccessPolicy, options.IotHubSharedAccessKey); /// /// IotHubServiceClient hubClient = new IotHubServiceClient(options.Endpoint, credential); /// </code> public IotHubServiceClient(Uri endpoint, IotHubSasCredential credential, IotHubServiceClientOptions options = default) : this(SetEndpointToIotHubSasCredential(endpoint, credential), options) { }