private static AccessKey BuildAadAccessKey(Dictionary <string, string> dict, string endpoint, int?port)
        {
            if (dict.ContainsKey(ClientIdProperty))
            {
                if (!dict.ContainsKey(TenantIdProperty))
                {
                    throw new ArgumentException(MissingTenantIdProperty, TenantIdProperty);
                }

                var options = new AadApplicationOptions(dict[ClientIdProperty], dict[TenantIdProperty]);

                if (dict.TryGetValue(ClientSecretProperty, out var clientSecret))
                {
                    return(new AadAccessKey(options.WithClientSecret(clientSecret), endpoint, port));
                }
                else if (dict.TryGetValue(ClientCertProperty, out var clientCert))
                {
                    if (!File.Exists(clientCert))
                    {
                        throw new FileNotFoundException(FileNotExists, clientCert);
                    }
                    var cert = new X509Certificate2(clientCert);
                    return(new AadAccessKey(options.WithClientCert(cert), endpoint, port));
                }
                else
                {
                    throw new ArgumentException(MissingClientSecretProperty, ClientSecretProperty);
                }
            }
            else
            {
                return(new AadAccessKey(new AadManagedIdentityOptions(), endpoint, port));
            }
        }
Esempio n. 2
0
        public static IConfidentialClientApplication BuildApplication(AadApplicationOptions options)
        {
            if (options == null)
            {
                throw new InvalidOperationException("Failed to build Azure Active Directory application. (disabled)");
            }

            var builder = ConfidentialClientApplicationBuilder.Create(options.ClientId)
                          .WithAuthority(options.BuildAuthority());

            if (options.ClientCert != null)
            {
                builder.WithCertificate(options.ClientCert);
            }
            else if (!string.IsNullOrEmpty(options.ClientSecret))
            {
                builder.WithClientSecret(options.ClientSecret);
            }
            else
            {
                throw new ArgumentNullException("Neither clientCert not clientSecret has been provided.");
            }
            return(builder.Build());
        }