locked
Get IoT module connection string (or primary key) with IoT hub connection string RRS feed

  • Question

  • Hi all,

    I want to get the IoT module connection string of a module on an edge device on an IoT hub.

    I have hub connection string (with key), and the device ID and module name.

    How I can obtain the connection string (or primary key, from which I can form the connection string). Thanks

    --Keshava


    • Edited by Keshava GN Thursday, December 5, 2019 12:14 PM
    Thursday, December 5, 2019 7:11 AM

Answers

  • Hi I hope you find this useful:

    The AddModuleAsync method creates a module identity with ID myFirstModule under device myFirstDevice. If that module ID already exists in the identity registry, the code simply retrieves the existing module information. The app then displays the primary key for that identity. You use this key in the simulated module app to connect to your hub.

    private static async Task AddModuleAsync()
    {
        RegistryManager registryManager = 
          RegistryManager.CreateFromConnectionString(connectionString);
        Module module;
    
        try
        {
            module = 
              await registryManager.AddModuleAsync(new Module(deviceID, moduleID));
        }
        catch (ModuleAlreadyExistsException)
        {
            module = await registryManager.GetModuleAsync(deviceID, moduleID);
        }
    
        Console.WriteLine("Generated module key: {0}", module.Authentication.SymmetricKey.PrimaryKey);
    }
    *Source: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-module-twin-getstarted#get-the-iot-hub-connection-string


    Tuesday, December 10, 2019 8:40 PM

All replies

  • In short, my query is, how to get IoT module primary key programmatically, when we have IoT hub connection string.

    Thanks.

    • Edited by Keshava GN Friday, December 6, 2019 6:44 AM
    Friday, December 6, 2019 6:43 AM
  • Hello Keshava,

    Apologies for the delay in response.

    I haven’t found a programmatic approach to get the module connection string. However, you can get the module connection string directly from the portal.

    Please follow the below steps in case if you haven’t checked earlier.

    Goto Azure Portal -> Select IoT Hub -> Select IoT edge device -> copy Primary Connection String

    Please refer the documentation for more details. Do let us know if that helps.

    Tuesday, December 10, 2019 5:31 PM
  • Hi I hope you find this useful:

    The AddModuleAsync method creates a module identity with ID myFirstModule under device myFirstDevice. If that module ID already exists in the identity registry, the code simply retrieves the existing module information. The app then displays the primary key for that identity. You use this key in the simulated module app to connect to your hub.

    private static async Task AddModuleAsync()
    {
        RegistryManager registryManager = 
          RegistryManager.CreateFromConnectionString(connectionString);
        Module module;
    
        try
        {
            module = 
              await registryManager.AddModuleAsync(new Module(deviceID, moduleID));
        }
        catch (ModuleAlreadyExistsException)
        {
            module = await registryManager.GetModuleAsync(deviceID, moduleID);
        }
    
        Console.WriteLine("Generated module key: {0}", module.Authentication.SymmetricKey.PrimaryKey);
    }
    *Source: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-module-twin-getstarted#get-the-iot-hub-connection-string


    Tuesday, December 10, 2019 8:40 PM
  • Yes. Got it... Thanks.

    We need to have only GetModuleAsync method since we know that the module exists. And if module is EdgeAgent (which is, in my case), we need only GetModuleAsync.

    Thanks.

    • Edited by Keshava GN Wednesday, December 11, 2019 8:10 AM
    Wednesday, December 11, 2019 8:07 AM
  • Thanks. I was able to do this using the link shared below.
    Wednesday, December 11, 2019 8:08 AM
  • Glad to hear that it was helpful.
    Wednesday, December 11, 2019 9:34 AM