Exemplo n.º 1
0
        public IActionResult RemoveDatabase()
        {
            _clientContext.RemoveRange(_clientContext.Clients);
            _scopeContext.RemoveRange(_scopeContext.Scopes);
            _operationalContext.RemoveRange(_operationalContext.Tokens);
            _operationalContext.RemoveRange(_operationalContext.Consents);

            _clientContext.SaveChanges();
            _scopeContext.SaveChanges();
            _operationalContext.SaveChanges();
            return(Json("Success"));
        }
        public void CanAddAndDeleteClientRedirectUri()
        {
            using (var db = new ClientConfigurationContext<int>(_options))
            {
                db.Clients.Add(new Client<int>
                {
                    ClientId = "test-client",
                    ClientName = "Test Client"
                });

                db.SaveChanges();
            }

            using (var db = new ClientConfigurationContext<int>(_options))
            {
                var client = db.Clients
                    .First();

                client.RedirectUris = new HashSet<ClientRedirectUri<int>>();
                client.RedirectUris.Add(new ClientRedirectUri<int>
                {
                    Uri = "https://redirect-uri-1"
                });

                db.SaveChanges();
            }

            using (var db = new ClientConfigurationContext<int>(_options))
            {
                var client = db.Clients
                    .Include(e => e.RedirectUris)
                    .First();
                var redirectUri = client.RedirectUris.First();

                client.RedirectUris.Remove(redirectUri);

                db.SaveChanges();
            }

            using (var db = new ClientConfigurationContext<int>(_options))
            {
                var client = db.Clients
                    .Include(e => e.RedirectUris)
                    .First();

                Assert.Equal(0, client.RedirectUris.Count());
            }
        }
        public void CanAddAndDeleteClientRedirectUri()
        {
            using (var db = new ClientConfigurationContext <int>(_options))
            {
                db.Clients.Add(new Client <int>
                {
                    ClientId   = "test-client",
                    ClientName = "Test Client"
                });

                db.SaveChanges();
            }

            using (var db = new ClientConfigurationContext <int>(_options))
            {
                var client = db.Clients
                             .First();

                client.RedirectUris = new HashSet <ClientRedirectUri <int> >();
                client.RedirectUris.Add(new ClientRedirectUri <int>
                {
                    Uri = "https://redirect-uri-1"
                });

                db.SaveChanges();
            }

            using (var db = new ClientConfigurationContext <int>(_options))
            {
                var client = db.Clients
                             .Include(e => e.RedirectUris)
                             .First();
                var redirectUri = client.RedirectUris.First();

                client.RedirectUris.Remove(redirectUri);

                db.SaveChanges();
            }

            using (var db = new ClientConfigurationContext <int>(_options))
            {
                var client = db.Clients
                             .Include(e => e.RedirectUris)
                             .First();

                Assert.Equal(0, client.RedirectUris.Count());
            }
        }
        public void CanAddAndDeleteClientScopes()
        {
            using (var db = new ClientConfigurationContext<int>(_options))
            {
                db.Clients.Add(new Client<int>
                {
                    ClientId = "test-client-scopes",
                    ClientName = "Test Client"
                });

                db.SaveChanges();
            }

            using (var db = new ClientConfigurationContext<int>(_options))
            {
                var client = db.Clients.First();

                client.AllowedScopes = new HashSet<ClientScope<int>>();
                client.AllowedScopes.Add(new ClientScope<int>
                {
                    Scope = "test"
                });

                db.SaveChanges();
            }

            using (var db = new ClientConfigurationContext<int>(_options))
            {
                var client = db.Clients
                    .Include(e => e.AllowedScopes)
                    .First();
                var scope = client.AllowedScopes.First();

                client.AllowedScopes.Remove(scope);

                db.SaveChanges();
            }

            using (var db = new ClientConfigurationContext<int>(_options))
            {
                var client = db.Clients
                    .Include(e => e.AllowedScopes)
                    .First();

                Assert.Equal(0, client.AllowedScopes.Count());
            }
        }
        public void CanAddAndDeleteClientScopes()
        {
            using (var db = new ClientConfigurationContext <int>(_options))
            {
                db.Clients.Add(new Client <int>
                {
                    ClientId   = "test-client-scopes",
                    ClientName = "Test Client"
                });

                db.SaveChanges();
            }

            using (var db = new ClientConfigurationContext <int>(_options))
            {
                var client = db.Clients.First();

                client.AllowedScopes = new HashSet <ClientScope <int> >();
                client.AllowedScopes.Add(new ClientScope <int>
                {
                    Scope = "test"
                });

                db.SaveChanges();
            }

            using (var db = new ClientConfigurationContext <int>(_options))
            {
                var client = db.Clients
                             .Include(e => e.AllowedScopes)
                             .First();
                var scope = client.AllowedScopes.First();

                client.AllowedScopes.Remove(scope);

                db.SaveChanges();
            }

            using (var db = new ClientConfigurationContext <int>(_options))
            {
                var client = db.Clients
                             .Include(e => e.AllowedScopes)
                             .First();

                Assert.Equal(0, client.AllowedScopes.Count());
            }
        }
        private void AddClients(Client ct)
        {
            var client = new Client <Guid>()
            {
                AbsoluteRefreshTokenLifetime = ct.AbsoluteRefreshTokenLifetime,
                AccessTokenLifetime          = ct.AccessTokenLifetime,
                AccessTokenType            = ct.AccessTokenType,
                AllowAccessToAllGrantTypes = ct.AllowAccessToAllCustomGrantTypes,
                AllowAccessToAllScopes     = ct.AllowAccessToAllScopes,
                AllowClientCredentialsOnly = ct.AllowClientCredentialsOnly,
                AllowPromptNone            = ct.AllowPromptNone,
                AllowRememberConsent       = ct.AllowRememberConsent,
                AlwaysSendClientClaims     = ct.AlwaysSendClientClaims,
                AuthorizationCodeLifetime  = ct.AuthorizationCodeLifetime,
                ClientId                    = ct.ClientId,
                ClientName                  = ct.ClientName,
                ClientUri                   = ct.ClientUri,
                EnableLocalLogin            = ct.EnableLocalLogin,
                Enabled                     = ct.Enabled,
                Flow                        = ct.Flow,
                IdentityTokenLifetime       = ct.IdentityTokenLifetime,
                IncludeJwtId                = ct.IncludeJwtId,
                LogoUri                     = ct.LogoUri,
                LogoutSessionRequired       = ct.LogoutSessionRequired,
                LogoutUri                   = ct.LogoutUri,
                PrefixClientClaims          = ct.PrefixClientClaims,
                RefreshTokenExpiration      = ct.RefreshTokenExpiration,
                RefreshTokenUsage           = ct.RefreshTokenUsage,
                RequireConsent              = ct.RequireConsent, //设置为true会有问题?
                SlidingRefreshTokenLifetime = ct.SlidingRefreshTokenLifetime,
                UpdateAccessTokenOnRefresh  = ct.UpdateAccessTokenClaimsOnRefresh
            };

            client.ClientSecrets = new List <ClientSecret <Guid> >();
            client.AllowedScopes = new List <ClientScope <Guid> >();
            client.RedirectUris  = new List <ClientRedirectUri <Guid> >();
            client.IdentityProviderRestrictions = new List <ClientProviderRestriction <Guid> >();
            client.PostLogoutRedirectUris       = new List <ClientPostLogoutRedirectUri <Guid> >();
            client.AllowedCustomGrantTypes      = new List <ClientCustomGrantType <Guid> >();
            client.Claims             = new List <ClientClaim <Guid> >();
            client.AllowedCorsOrigins = new List <ClientCorsOrigin <Guid> >();

            //Add Secrets
            foreach (var secret in ct.ClientSecrets)
            {
                var clientSecret = new ClientSecret <Guid>()
                {
                    Description = secret.Description,
                    Expiration  = secret.Expiration.HasValue ? (DateTime?)secret.Expiration.Value.DateTime : null,
                    Type        = secret.Type,
                    Value       = secret.Value
                };
                client.ClientSecrets.Add(clientSecret);
            }
            //Add Scopes
            foreach (var scope in ct.AllowedScopes)
            {
                var allowedScope = new ClientScope <Guid>()
                {
                    Scope = scope
                };
                client.AllowedScopes.Add(allowedScope);
            }

            //Add RedirectUris
            foreach (var redirectUri in ct.RedirectUris)
            {
                var clientRedirectUri = new ClientRedirectUri <Guid>()
                {
                    Uri = redirectUri
                };
                client.RedirectUris.Add(clientRedirectUri);
            }
            //Add ProviderRestrictions
            foreach (var provider in ct.IdentityProviderRestrictions)
            {
                var clientProviderRestriction = new ClientProviderRestriction <Guid>()
                {
                    Provider = provider
                };
                client.IdentityProviderRestrictions.Add(clientProviderRestriction);
            }
            //Add ClientPostLogoutRedirectUri
            foreach (var postLogoutRedirectUri in ct.PostLogoutRedirectUris)
            {
                var clientPostLogoutRedirectUri = new ClientPostLogoutRedirectUri <Guid>()
                {
                    Uri = postLogoutRedirectUri
                };
                client.PostLogoutRedirectUris.Add(clientPostLogoutRedirectUri);
            }
            //Add ClientCustomGrantTypes
            foreach (var customGrantType in ct.AllowedCustomGrantTypes)
            {
                var clientCustomerGrantType = new ClientCustomGrantType <Guid>()
                {
                    GrantType = customGrantType
                };
                client.AllowedCustomGrantTypes.Add(clientCustomerGrantType);
            }
            //Add ClientClaims
            foreach (var claim in ct.Claims)
            {
                var clientClaim = new ClientClaim <Guid>()
                {
                    Type  = claim.Type,
                    Value = claim.Value
                };
                client.Claims.Add(clientClaim);
            }
            //Add ClientCorsOrigins
            foreach (var corsOrigin in ct.AllowedCorsOrigins)
            {
                var clientCorsOrigin = new ClientCorsOrigin <Guid>()
                {
                    Origin = corsOrigin
                };
                client.AllowedCorsOrigins.Add(clientCorsOrigin);
            }
            _clientContext.Clients.Add(client);
            _clientContext.SaveChanges();
        }