Search code examples
azure-iot-hubazure-iot-sdk

IoT .Net SDK: Is there a way to set the SAS Token Life?


Using the .Net SDK for IoT, is there a way to set the OPTION_SAS_TOKEN_LIFETIME (https://github.com/Azure/azure-iot-sdk-c/blob/master/iothub_client/inc/iothub_client_options.h#L36)?

I know I can generate the token and own the process using:

            var sasBuilder = new SharedAccessSignatureBuilder();
            sasBuilder.Key = "jzaOR************F2uT1c=";
            sasBuilder.Target = "k****y.azure-devices.net" + "/devices/" + "dotNet";
            sasBuilder.TimeToLive = TimeSpan.FromMinutes(120);

            var auth = new DeviceAuthenticationWithToken("dotNet", sasBuilder.ToSignature());
            deviceClient = DeviceClient.Create("k****y.azure-devices.net", auth, TransportType.Mqtt);

but this would assume I own the SAS Token expiration.

I also see that I can set it in the C SDK: (https://github.com/Azure/azure-iot-sdk-c/issues/1182)


Solution

  • See the related thread in github: https://github.com/Azure/azure-iot-sdk-csharp/issues/1818

    "By default, if you initialize the device client using connection string, it will generate sas tokens that are valid for an hour. To override this behavior, you can implement the abstract class DeviceAuthenticationWithTokenRefresh and pass it to the device client Create method for initialization."

    Thanks.