Exemplo n.º 1
0
        static async Task <LeafDevice> CreateWithSasAsync(
            string leafDeviceId,
            Option <string> parentId,
            IotHub iotHub,
            ITransportSettings transport,
            string edgeHostname,
            CancellationToken token,
            ClientOptions options,
            bool nestedEdge)
        {
            Device leaf = new Device(leafDeviceId)
            {
                Authentication = new AuthenticationMechanism
                {
                    Type = AuthenticationType.Sas
                }
            };

            await parentId.ForEachAsync(
                async p =>
            {
                Device edge = await GetEdgeDeviceIdentityAsync(p, iotHub, token);
                leaf.Scope  = edge.Scope;
            });

            // @To Remove this is a hack to be able to create lea. See PBI: 9171870
            string hostname = iotHub.Hostname;

            if (nestedEdge)
            {
                hostname = edgeHostname;
            }

            leaf = await iotHub.CreateDeviceIdentityAsync(leaf, token);

            return(await DeleteIdentityIfFailedAsync(
                       leaf,
                       iotHub,
                       token,
                       () =>
            {
                string connectionString =
                    $"HostName={hostname};" +
                    $"DeviceId={leaf.Id};" +
                    $"SharedAccessKey={leaf.Authentication.SymmetricKey.PrimaryKey};" +
                    $"GatewayHostName={edgeHostname}";

                return CreateLeafDeviceAsync(
                    leaf,
                    () => DeviceClient.CreateFromConnectionString(connectionString, new[] { transport }, options),
                    iotHub,
                    token);
            }));
        }
Exemplo n.º 2
0
        static async Task <LeafDevice> CreateWithCaCertAsync(
            string leafDeviceId,
            string parentId,
            CertificateAuthority ca,
            IotHub iotHub,
            ITransportSettings transport,
            string edgeHostname,
            CancellationToken token,
            ClientOptions options)
        {
            Device edge = await GetEdgeDeviceIdentityAsync(parentId, iotHub, token);

            Device leaf = new Device(leafDeviceId)
            {
                Authentication = new AuthenticationMechanism
                {
                    Type = AuthenticationType.CertificateAuthority
                },
                Scope = edge.Scope
            };

            leaf = await iotHub.CreateDeviceIdentityAsync(leaf, token);

            return(await DeleteIdentityIfFailedAsync(
                       leaf,
                       iotHub,
                       token,
                       async() =>
            {
                IdCertificates certFiles = await ca.GenerateIdentityCertificatesAsync(leafDeviceId, token);

                (X509Certificate2 leafCert, IEnumerable <X509Certificate2> trustedCerts) =
                    CertificateHelper.GetServerCertificateAndChainFromFile(certFiles.CertificatePath, certFiles.KeyPath);
                // .NET runtime requires that we install the chain of CA certs, otherwise it can't
                // provide them to a server during authentication.
                OsPlatform.Current.InstallTrustedCertificates(trustedCerts);

                return await CreateLeafDeviceAsync(
                    leaf,
                    () => DeviceClient.Create(
                        iotHub.Hostname,
                        edgeHostname,
                        new DeviceAuthenticationWithX509Certificate(leaf.Id, leafCert),
                        new[] { transport },
                        options),
                    iotHub,
                    token);
            }));