Exemplo n.º 1
0
        private static async Task <EventGridEdgeClient> GetEventGridClientAsync(GridConfiguration gridConfig)
        {
            using var securityDaemonClient = new SecurityDaemonClient();

            // get the client certificate to use when communicating with eventgrid
            (X509Certificate2 clientCertificate, X509Certificate2[] chain) = await securityDaemonClient.GetIdentityCertificateAsync().ConfigureAwait(false);

            Console.WriteLine($"Client Certificate issue is valid from {clientCertificate.NotBefore}, {clientCertificate.NotAfter}");
            string[] urlTokens = gridConfig.Url.Split(":");
            if (urlTokens.Length != 3)
            {
                throw new Exception($"URL should be of the form '<protocol>://<moduleName>:<portNo>' ");
            }

            string baseUrl = urlTokens[0] + ":" + urlTokens[1];
            int    port    = int.Parse(urlTokens[2], CultureInfo.InvariantCulture);

            return(new EventGridEdgeClient(baseUrl, port, new CustomHttpClientFactory(chain[0], clientCertificate)));
        }
Exemplo n.º 2
0
        private static async Task <EventGridEdgeClient> GetEventGridClientAsync(GridConfiguration gridConfig)
        {
            string[] urlTokens = gridConfig.Url.Split(":");
            if (urlTokens.Length != 3)
            {
                throw new Exception($"URL should be of the form '<protocol>://<moduleName>:<portNo>' ");
            }

            string baseUrl = urlTokens[0] + ":" + urlTokens[1];
            int    port    = int.Parse(urlTokens[2], CultureInfo.InvariantCulture);

            if (gridConfig.ClientAuth.Source.Equals("IoTEdge", StringComparison.OrdinalIgnoreCase))
            {
                SecurityDaemonClient iotSecurity = new SecurityDaemonClient();
                (X509Certificate2 identityCertificate, IEnumerable <X509Certificate2> chain) = await iotSecurity.GetIdentityCertificateAsync();

                return(new EventGridEdgeClient(baseUrl, port, new CustomHttpClientFactory(chain.First(), identityCertificate)));
            }
            else if (gridConfig.ClientAuth.Source.Equals("BearerToken", StringComparison.OrdinalIgnoreCase))
            {
                EventGridEdgeClient egClient = new EventGridEdgeClient(baseUrl, port);

                HttpRequestHeaders defaultMgmtRequestHeaders = egClient.HttpClient.DefaultRequestHeaders;
                defaultMgmtRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", $"{gridConfig.ClientAuth.Token1}");

                HttpRequestHeaders defaultRuntimeRequestHeaders = egClient.HttpClient.DefaultRequestHeaders;
                defaultRuntimeRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", $"{gridConfig.ClientAuth.Token1}");
            }

            throw new Exception("Cannot create eventgrid client!");
        }