public static CertificateOptions FromConfiguration(IConfiguration configuration)
        {
            var options = new CertificateOptions();

            if (configuration.GetSection("certificates").GetChildren().Count() > 0)
            {
                foreach (var child in configuration.GetSection("certificates:stores").GetChildren())
                {
                    var store = CertificateStoreConfiguration.FromSection(child);
                    if (store != null)
                    {
                        options.AddStore(store);
                    }
                }
            }

            return(options);
        }
        public void AddStore(ICertificateStoreConfiguration store)
        {
            try {
                if (!string.IsNullOrEmpty(store.Path))
                {
                    var p = PathUtil.GetFullPath(store.Path);
                    store = new CertificateStoreConfiguration()
                    {
                        Name          = store.Name,
                        StoreLocation = store.StoreLocation,
                        Claims        = store.Claims,
                        Path          = p
                    };
                }

                if (string.IsNullOrEmpty(store.Name))
                {
                    throw new ArgumentNullException("Name");
                }
            }
            catch (ArgumentNullException e) {
                Log.Error(e, $"Invalid store name in certificate options. Name must not be empty.");
                throw;
            }
            catch (ArgumentException e) {
                Log.Error(e, $"Invalid store path '{store.Path}' in certificate options.");
                throw;
            }

            //
            // Replace the store if it exists
            int existing = _stores.FindIndex(s => s.Name.Equals(store.Name, StringComparison.OrdinalIgnoreCase));

            if (existing != -1)
            {
                _stores[existing] = store;
            }
            else
            {
                _stores.Add(store);
            }
        }
Esempio n. 3
0
        public static CertificateStoreConfiguration FromSection(IConfigurationSection section)
        {
            string        name   = section.GetValue("name", string.Empty);
            string        path   = section.GetValue <string>("path");
            List <string> claims = new List <string>();

            ConfigurationBinder.Bind(section.GetSection("claims"), claims);

            CertificateStoreConfiguration store = null;

            if (!string.IsNullOrEmpty(name))
            {
                store = new CertificateStoreConfiguration()
                {
                    Name          = name,
                    Path          = path,
                    Claims        = claims,
                    StoreLocation = StoreLocation.LocalMachine
                };
            }

            return(store);
        }