コード例 #1
0
        public async Task Test_Create_Add_Delete_ClientSecrets_ByClientIdAsync()
        {
            var adminStore = new IdentityServer3AdminStore();
            var insert     = await CassandraTestHelper.InsertTestData_Clients(1);

            var result = await adminStore.FindClientByIdAsync(insert[0].ClientId);

            Assert.IsNotNull(result);
            Assert.AreEqual(insert[0].ClientName, result.ClientName);

            var original = result.ClientSecrets;

            List <Secret> newSecrets = new List <Secret>();

            for (int i = 0; i < 2; ++i)
            {
                newSecrets.Add(new Secret()
                {
                    Value       = Guid.NewGuid().ToString(),
                    Description = Guid.NewGuid().ToString(),
                    Expiration  = DateTimeOffset.UtcNow.AddHours(1),
                    Type        = Guid.NewGuid().ToString()
                });
            }
            var finalList = new List <Secret>();

            finalList.AddRange(original);
            finalList.AddRange(newSecrets);

            await adminStore.AddClientSecretsToClientAsync(insert[0].ClientId, newSecrets);

            result = await adminStore.FindClientByIdAsync(insert[0].ClientId);

            Assert.IsNotNull(result);
            Assert.AreEqual(result.ClientSecrets.Count(), finalList.Count);

            var ff = result.ClientSecrets.Except(finalList, SecretComparer.OrdinalIgnoreCase);

            Assert.IsFalse(ff.Any());

            await adminStore.DeleteClientSecretsFromClientAsync(insert[0].ClientId, result.ClientSecrets);

            result = await adminStore.FindClientByIdAsync(insert[0].ClientId);

            Assert.IsNotNull(result);

            Assert.IsFalse(result.ClientSecrets.Any());
        }
コード例 #2
0
        public async Task <ActionResult> Secret(SecretViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var    adminStore            = new IdentityServer3AdminStore();
            var    myCrypto              = new TripleDesEncryption(model.PassCode);
            var    protectedClientSecret = myCrypto.Encrypt(model.OpenClientSecret);
            Secret secret = null;

            if (string.Compare(model.SecretType, "SharedSecret", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var hashedClientSecret = model.OpenClientSecret.Sha256();
                secret = new Secret(hashedClientSecret)
                {
                    Type = model.SecretType
                };
                ProtectedSecretHandle protectedSecretHandle = new ProtectedSecretHandle()
                {
                    ClientId       = model.ClientId,
                    Value          = hashedClientSecret,
                    ProtectedValue = protectedClientSecret
                };
                await adminStore.AddSecretProtectedValue(protectedSecretHandle);
            }
            if (string.Compare(model.SecretType, "X509Thumbprint", StringComparison.OrdinalIgnoreCase) == 0)
            {
                secret = new Secret(model.ThumbPrint)
                {
                    Type = model.SecretType
                };
            }

            var secrets = new List <Secret> {
                secret
            };
            await adminStore.AddClientSecretsToClientAsync(model.ClientId, secrets);

            return(RedirectToAction("Index"));
        }