public void Setup()
        {
            AdcsTemplate result = null;

            try
            {
                result = configDb.Get <AdcsTemplate>(x => x.Cipher == CipherAlgorithm.RSA && x.WindowsApi == WindowsApi.CryptoApi && x.KeyUsage == KeyUsage.ServerAuthentication).First();
                //result = configDb.GetAdcsTemplate(HashAlgorithm.SHA256, CipherAlgorithm.RSA, WindowsApi.CryptoApi, KeyUsage.ServerAuthentication);
            }
            catch
            {
                if (result == null)
                {
                    configDb.Insert <AdcsTemplate>(new AdcsTemplate()
                    {
                        WindowsApi = WindowsApi.CryptoApi,
                        Cipher     = CipherAlgorithm.RSA,
                        //Hash = HashAlgorithm.SHA256,
                        KeyUsage = KeyUsage.ServerAuthentication,
                        Name     = "ServerAuthentication-CapiRsa"
                    });
                }
            }
            ActiveDirectoryMetadata idp = configDb.GetAll <ActiveDirectoryMetadata>().FirstOrDefault();

            configDb.DropCollection <PrivateCertificateAuthorityConfig>();

            PrivateCertificateAuthorityConfig caConfig = new PrivateCertificateAuthorityConfig()
            {
                CommonName         = caCommonName,
                ServerName         = caServerName,
                HashAlgorithm      = HashAlgorithm.SHA256,
                IdentityProviderId = idp.Id
            };
        }
        public void InitializeTest()
        {
            user = new Mock <ClaimsPrincipal>();

            string configPath = Path.GetTempFileName();

            configDb = new LiteDbConfigurationRepository(configPath);

            configDb.Insert <AdcsTemplate>(new AdcsTemplate()
            {
                WindowsApi = WindowsApi.CryptoApi,
                Cipher     = CipherAlgorithm.RSA,
                //Hash = HashAlgorithm.SHA256,
                KeyUsage = KeyUsage.ServerAuthentication,
                Name     = "ServerAuthentication-CapiRsa"
            });

            configDb.Insert <AdcsTemplate>(new AdcsTemplate()
            {
                WindowsApi = WindowsApi.CryptoApi,
                Cipher     = CipherAlgorithm.RSA,
                //Hash = HashAlgorithm.SHA256,
                KeyUsage = KeyUsage.None,
                Name     = "NoKeyUsage-CapiRsa"
            });

            configDb.Insert <AdcsTemplate>(new AdcsTemplate()
            {
                WindowsApi = WindowsApi.Cng,
                Cipher     = CipherAlgorithm.RSA,
                // Hash = HashAlgorithm.SHA256,
                KeyUsage = KeyUsage.ServerAuthentication,
                Name     = "ServerAuthentication-CngRsa"
            });

            configDb.Insert <AdcsTemplate>(new AdcsTemplate()
            {
                WindowsApi = WindowsApi.Cng,
                Cipher     = CipherAlgorithm.RSA,
                //Hash = HashAlgorithm.SHA256,
                KeyUsage = KeyUsage.ServerAuthentication | KeyUsage.ClientAuthentication,
                Name     = "ClientServerAuthentication-CngRsa"
            });

            configDb.Insert <AdcsTemplate>(new AdcsTemplate()
            {
                WindowsApi = WindowsApi.Cng,
                Cipher     = CipherAlgorithm.ECDH,
                //Hash = HashAlgorithm.SHA256,
                KeyUsage = KeyUsage.ServerAuthentication,
                Name     = "ServerAuthentication-CngEcdh"
            });

            configDb.Insert <AdcsTemplate>(new AdcsTemplate()
            {
                WindowsApi = WindowsApi.Cng,
                Cipher     = CipherAlgorithm.ECDSA,
                //Hash = HashAlgorithm.SHA256,
                KeyUsage = KeyUsage.ServerAuthentication,
                Name     = "ServerAuthentication-CngEcdsa"
            });

            Logic.SecretKeyProvider secretKeyProvider = new Logic.SecretKeyProvider();

            AppConfig appConfig = new AppConfig()
            {
                EncryptionKey = secretKeyProvider.NewSecretBase64(32)
            };

            configDb.SetAppConfig(appConfig);


            ActiveDirectoryMetadata identitySource = new ActiveDirectoryMetadata()
            {
                Domain  = "cm.local",
                Enabled = true,
                ActiveDirectoryMetadataType = ActiveDirectoryMetadataType.ActiveDirectoryBasic,
                Id         = Guid.NewGuid(),
                Name       = "cm.local",
                Password   = password,
                Username   = username,
                SearchBase = "DC=cm,DC=local"
            };


            PrivateCertificateAuthorityConfig caConfig = new PrivateCertificateAuthorityConfig()
            {
                CommonName         = caCommonName,
                ServerName         = caServerName,
                HashAlgorithm      = HashAlgorithm.SHA256,
                Id                 = Guid.NewGuid(),
                IdentityProviderId = identitySource.Id
            };

            configDb.Insert <PrivateCertificateAuthorityConfig>(caConfig);

            configDb.Insert <ActiveDirectoryMetadata>(identitySource);


            templateLogic = new AdcsTemplateLogic(configDb, activeDirectory);
            KeyUsage keyUsage = KeyUsage.ClientAuthentication | KeyUsage.ServerAuthentication;
            //AdcsTemplate config = configDb.Get<AdcsTemplate>(x => x.Cipher == CipherAlgorithm.RSA && x.WindowsApi == WindowsApi.Cng && x.KeyUsage == keyUsage).First();

            //var config = configDb.GetAdcsTemplate(HashAlgorithm.SHA256, CipherAlgorithm.RSA, WindowsApi.Cng, KeyUsage.ClientAuthentication | KeyUsage.ServerAuthentication);

            string certDbPath = Path.GetTempFileName();

            certDb = new LiteDbCertificateRepository(certDbPath);
        }