Exemple #1
0
        private static void EnsureSeedData(IConfigurationDbContext context)
        {
            //if (!context.Clients.Any())
            //{
            Log.Debug("Clients being populated");
            foreach (var client in Config.Clients.ToList())
            {
                Client foundClient = context.Clients.FirstOrDefault(c => c.ClientId == client.ClientId);
                if (foundClient != null)
                {
                    context.Clients.Remove(foundClient);
                }
                context.Clients.Add(client.ToEntity());
            }
            context.SaveChanges();
            //}
            //else
            //{
            //    Log.Debug("Clients already populated");
            //}

            //if (!context.IdentityResources.Any())
            //{
            Log.Debug("IdentityResources being populated");
            foreach (var resource in Config.Ids.ToList())
            {
                var foundIR = context.IdentityResources.FirstOrDefault(i => i.Name == resource.Name);
                if (foundIR != null)
                {
                    context.IdentityResources.Remove(foundIR);
                }
                context.IdentityResources.Add(resource.ToEntity());
            }
            context.SaveChanges();
            //}
            //else
            //{
            //    Log.Debug("IdentityResources already populated");
            //}

            //if (!context.ApiResources.Any())
            //{
            Log.Debug("ApiResources being populated");
            foreach (IdentityServer4.Models.ApiResource resource in Config.Apis.ToList())
            {
                ApiResource foundApiResource = context.ApiResources.FirstOrDefault(a => a.Name == resource.Name);
                if (foundApiResource != null)
                {
                    context.ApiResources.Remove(foundApiResource);
                }

                context.ApiResources.Add(resource.ToEntity());
            }
            context.SaveChanges();
            //}
            //else
            //{
            //    Log.Debug("ApiResources already populated");
            //}
        }
Exemple #2
0
        private static void InitCustomSeedData(IConfigurationDbContext context)
        {
            if (!context.Clients.Any())
            {
                foreach (var client in PasswordInitConfig.GetClients())
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }
            if (!context.ApiResources.Any())
            {
                foreach (var api in PasswordInitConfig.GetApiResources())
                {
                    context.ApiResources.Add(api.ToEntity());
                }
                context.SaveChanges();
            }

            //if (!context.IdentityResources.Any())
            //{
            //    foreach (var id in PasswordInitConfig.GetIdentityResources())
            //        context.IdentityResources.Add(id.ToEntity());
            //    context.SaveChanges();
            //}

            //init User
        }
        private void SeedIdentityServerConfigData(IConfigurationDbContext context)
        {
            if (!context.Clients.Any())
            {
                var clientUrls = Startup.Configuration["ClientUrls"];

                foreach (var client in Config.GetClients(clientUrls).ToList())
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }

            if (!context.IdentityResources.Any())
            {
                foreach (var resource in Config.GetIdentityResources().ToList())
                {
                    context.IdentityResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }

            if (!context.ApiResources.Any())
            {
                foreach (var resource in Config.GetApiResources().ToList())
                {
                    context.ApiResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
        }
        private static void EnsureSeedData(IConfigurationDbContext context)
        {
            if (!context.Clients.Any())
            {
                foreach (var client in Clients.Get().ToList())
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }

            if (!context.IdentityResources.Any())
            {
                foreach (var resource in Resources.GetIdentityResources().ToList())
                {
                    context.IdentityResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }

            if (!context.ApiResources.Any())
            {
                foreach (var resource in Resources.GetApiResources().ToList())
                {
                    context.ApiResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
        }
        private static void EnsureSeedData(IConfigurationDbContext context)
        {
            if (!context.Clients.Any())
            {
                Log.Debug("Users being populated");
                foreach (var client in Config.Clients.ToList())
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Log.Debug("Users already populated");
            }
            if (!context.Clients.Any())
            {
                Log.Debug("Clients being populated");
                foreach (var client in Config.Clients.ToList())
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Log.Debug("Clients already populated");
            }

            if (!context.IdentityResources.Any())
            {
                Log.Debug("IdentityResources being populated");
                foreach (var resource in Config.Ids.ToList())
                {
                    context.IdentityResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Log.Debug("IdentityResources already populated");
            }

            if (!context.ApiResources.Any())
            {
                Log.Debug("ApiResources being populated");
                foreach (var resource in Config.Apis.ToList())
                {
                    context.ApiResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Log.Debug("ApiResources already populated");
            }
        }
Exemple #6
0
        public IActionResult CreatePc(Client client)
        {
            //var AllowedGrantTypes = Request.Form["AllowedGrantTypes"];
            //var AllowedScopes = Request.Form["AllowedScopes"];

            ViewBag.msg = "";
            //是否已经添加过
            var userId = int.Parse(User.FindFirstValue("sub"));
            //记录用户申请的授权
            var user = _applicationDbContext.userClients.Count(_ => _.UserId == userId && _.Type == ApplicationType.web.ToString());

            if (user >= 2) //暂时只能申请2个
            {
                ViewBag.msg = "只能创建一个web应用";
            }
            else
            {
                //client.ClientName = User.FindFirstValue("email");
                client.RedirectUris = new string[] { client.ClientUri + new PathString("/signin-oidc") };
                //client.RedirectUris = new string[] { client.ClientUri + "signin-oidc" };
                client.PostLogoutRedirectUris           = new string[] { client.ClientUri + new PathString("/signout-callback-oidc") };
                client.AlwaysIncludeUserClaimsInIdToken = true;
                client.AllowOfflineAccess = true;
                client.AllowedCorsOrigins = new string[] { client.ClientUri };
                //client.IncludeJwtId = true;
                client.ClientId = Utils.GetRandomNum(10);
                var secrets = Utils.GetRandom(10);
                client.ClientSecrets = new List <Secret> {
                    new Secret(secrets.Sha256())     //secret
                };
                client.AlwaysSendClientClaims = true;
                client.Claims = new List <Claim> {
                    new Claim(JwtClaimTypes.Role, "thirdParty"), // 第三方角色
                    new Claim("secret", secrets),                //把密钥保存起来
                    //new Claim("source","web"),
                    //new Claim("name",User.FindFirstValue("email")), //一个用户只能创建一种类型
                };
                //client.Enabled = false; //如果要审核,就false,因为要审核
                var entity = _configurationDbContext.Clients.Add(client.ToEntity()).Entity;
                var rows   = _configurationDbContext.SaveChanges();
                if (rows > 0)
                {
                    //记录用户申请的授权
                    _applicationDbContext.userClients.Add(new UserClient
                    {
                        ClientId = entity.Id,
                        UserId   = userId,
                        Type     = ApplicationType.web.ToString()
                    });
                    _applicationDbContext.SaveChanges();
                }
            }
            return(RedirectToAction(nameof(Index)));
            //return View(nameof(Index));
        }
Exemple #7
0
        private static void EnsureSeedData(IConfigurationDbContext context)
        {
            Console.WriteLine("Seeding database...");

            var clientUrls = new Dictionary <string, string>();


            //不会执行seeddata  clientUrls为null

            if (!context.Clients.Any())
            {
                Console.WriteLine("Clients being populated");
                foreach (var client in Config.GetClients(clientUrls).ToList())
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Console.WriteLine("Clients already populated");
            }

            if (!context.IdentityResources.Any())
            {
                Console.WriteLine("IdentityResources being populated");
                foreach (var resource in Config.GetIdentityResources().ToList())
                {
                    context.IdentityResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Console.WriteLine("IdentityResources already populated");
            }

            if (!context.ApiResources.Any())
            {
                Console.WriteLine("ApiResources being populated");
                foreach (var resource in Config.GetApis().ToList())
                {
                    context.ApiResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Console.WriteLine("ApiResources already populated");
            }

            Console.WriteLine("Done seeding database.");
            Console.WriteLine();
        }
Exemple #8
0
        private static void SeedConfigurationDb(IConfigurationDbContext context, IConfiguration config)
        {
            Console.WriteLine("Seeding database...");


            if (!context.Clients.Any())
            {
                Console.WriteLine("Clients being populated");
                foreach (var client in Config.GetClients(config).ToList())
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Console.WriteLine("Clients already populated");
            }

            if (!context.IdentityResources.Any())
            {
                Console.WriteLine("IdentityResources being populated");
                foreach (var resource in Config.GetIdentityResources().ToList())
                {
                    context.IdentityResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Console.WriteLine("IdentityResources already populated");
            }

            if (!context.ApiResources.Any())
            {
                Console.WriteLine("ApiResources being populated");
                foreach (var resource in Config.GetApis(config).ToList())
                {
                    context.ApiResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Console.WriteLine("ApiResources already populated");
            }

            Console.WriteLine("Done seeding database.");
            Console.WriteLine();
        }
        private static void SeedConfigurations(IConfigurationDbContext context)
        {
            if (!context.Clients.Any())
            {
                Log.Debug("Clients being populated");
                foreach (var client in Config.Clients.ToList())
                {
                    var clientEntity = client.ToEntity();
                    context.Clients.Add(clientEntity);
                    Log.Debug("Adding client: {0}", clientEntity);
                }
                context.SaveChanges();
            }
            else
            {
                Log.Debug("Clients already populated");
            }

            if (!context.IdentityResources.Any())
            {
                Log.Debug("IdentityResources being populated");
                foreach (var resource in Config.Ids.ToList())
                {
                    var resourceEntity = resource.ToEntity();
                    context.IdentityResources.Add(resourceEntity);
                    Log.Debug("Adding Identity Resource: {0}", resourceEntity);
                }
                context.SaveChanges();
            }
            else
            {
                Log.Debug("IdentityResources already populated");
            }

            if (!context.ApiResources.Any())
            {
                Log.Debug("ApiResources being populated");
                foreach (var api in Config.Apis.ToList())
                {
                    var apiEntity = api.ToEntity();
                    context.ApiResources.Add(apiEntity);
                    Log.Debug("Adding Api Resource: {0}", apiEntity);
                }
                context.SaveChanges();
            }
            else
            {
                Log.Debug("ApiResources already populated");
            }
        }
Exemple #10
0
        public IActionResult NewModule(CLGE_CORE.Models.ModuleViewModel property, int?clientId)
        {
            if (ModelState.IsValid)
            {
                var clientApp = _configDbContext.Clients.Find(clientId);
                clientApp.Properties = new List <ClientProperty> ();
                clientApp.Properties.Add(new ClientProperty {
                    Key = property.Key, Value = property.Value
                });
                _configDbContext.SaveChanges();
            }

            //TODO: Implement Realistic Implementation
            return(RedirectToAction("Modules", new { clientId = clientId }));
        }
Exemple #11
0
        private static void EnsureSeedData(IConfigurationDbContext context)
        {
            Console.WriteLine("Seeding database...");

            context.Clients.RemoveRange(context.Clients);
            context.IdentityResources.RemoveRange(context.IdentityResources);
            context.ApiResources.RemoveRange(context.ApiResources);

            Console.WriteLine("Clients being populated");
            foreach (var client in Config.GetClients())
            {
                context.Clients.Add(client.ToEntity());
            }

            Console.WriteLine("IdentityResources being populated");
            foreach (var resource in Config.GetIdentityResources())
            {
                context.IdentityResources.Add(resource.ToEntity());
            }

            Console.WriteLine("ApiResources being populated");
            foreach (var resource in Config.GetApis())
            {
                context.ApiResources.Add(resource.ToEntity());
            }
            context.SaveChanges();

            Console.WriteLine("Done seeding database.");
            Console.WriteLine();
        }
Exemple #12
0
        internal virtual void EnsureSeedData()
        {
            if (!_configurationDbContext.IdentityResources.Any())
            {
                var resources = JsonConvert.DeserializeObject <List <IdentityResource> >(
                    File.ReadAllText(Path.Combine(_options.SeedExampleDataPath, "data_resources_identity.json")));
                foreach (var resource in resources)
                {
                    _configurationDbContext.IdentityResources.Add(resource.ToEntity());
                }
                _configurationDbContext.SaveChanges();
            }

            if (!_configurationDbContext.ApiResources.Any())
            {
                var resources = JsonConvert.DeserializeObject <List <ApiResource> >(
                    File.ReadAllText(Path.Combine(_options.SeedExampleDataPath, "data_resources_api.json")));
                foreach (var resource in resources)
                {
                    _configurationDbContext.ApiResources.Add(resource.ToEntity());
                }
                _configurationDbContext.SaveChanges();
            }

            if (!_configurationDbContext.Clients.Any())
            {
                var clients = JsonConvert.DeserializeObject <List <Client> >(
                    File.ReadAllText(Path.Combine(_options.SeedExampleDataPath, "data_clients.json")));
                foreach (var client in clients)
                {
                    _configurationDbContext.Clients.Add(client.ToEntity());
                }
                _configurationDbContext.SaveChanges();
            }

            if (!_userAccountDbContext.UserAccounts.Any())
            {
                var userAccounts = JsonConvert.DeserializeObject <List <UserAccount> >(
                    File.ReadAllText(Path.Combine(_options.SeedExampleDataPath, "data_users.json")));
                foreach (var userAccount in userAccounts)
                {
                    _userAccountDbContext.UserAccounts.Add(userAccount.ToEntity());
                }
                _userAccountDbContext.SaveChanges();
            }
        }
        public static void SeedConfiguration(IConfigurationDbContext context)
        {
            Log.Information("Seeding Identity Server configuration");
            if (!context.Clients.Any())
            {
                context.Clients.Add(new Client
                {
                    ClientId   = "spa",
                    ClientName = "SPA Client",
                    ClientUri  = "http://localhost:4200",

                    AllowedGrantTypes           = GrantTypes.Implicit,
                    AllowAccessTokensViaBrowser = true,
                    RequireConsent = false,
                    RedirectUris   =
                    {
                        "http://localhost:4200",
                        "http://localhost:4200/auth-callback",
                        "http://localhost:4200/silent.html",
                    },
                    PostLogoutRedirectUris = { "http://localhost:4200" },
                    AllowedCorsOrigins     = { "http://localhost:4200" },
                    AllowedScopes          = { "openid", "profile", "api" }
                }.ToEntity());
                context.SaveChanges();
            }

            if (!context.IdentityResources.Any())
            {
                context.IdentityResources.AddRange(new IdentityResource[]
                {
                    new IdentityResources.OpenId(),
                    new IdentityResources.Profile()
                }.Select(r => r.ToEntity()));
                context.SaveChanges();
            }

            if (!context.ApiResources.Any())
            {
                var apiResource = new ApiResource("api", "Application api");
                context.ApiResources.Add(apiResource.ToEntity());
                context.SaveChanges();
            }
        }
        private static void EnsureSeedData(IConfigurationDbContext context)
        {
            if (!context.Clients.Any())
            {
                Console.WriteLine("Clients being populated");
                foreach (var client in Config.GetClients().ToList())
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Console.WriteLine("Clients already populated");
            }

            if (!context.IdentityResources.Any())
            {
                Console.WriteLine("IdentityResources being populated");
                foreach (var resource in Config.GetIdentityResources().ToList())
                {
                    context.IdentityResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Console.WriteLine("IdentityResources already populated");
            }

            if (!context.ApiResources.Any())
            {
                Console.WriteLine("ApiResources being populated");
                foreach (var resource in Config.GetApiResources().ToList())
                {
                    context.ApiResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Console.WriteLine("ApiResources already populated");
            }
        }
        // GET: /<controller>/
        public IActionResult Index()
        {
            _dbContext.ApiResources.Add(new IdentityServer4.EntityFramework.Entities.ApiResource()
            {
                Name = "Abc", DisplayName = "Abc"
            });
            _dbContext.SaveChanges();
            var apiResources = _dbContext.ApiResources.ToList();

            return(View(apiResources));
        }
Exemple #16
0
        private static void EnsureSeedData(IConfigurationDbContext context)
        {
            if (!context.Clients.Any())
            {
                foreach (var client in Config.Clients)
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }

            if (!context.ApiResources.Any())
            {
                foreach (var api in Config.Apis)
                {
                    context.ApiResources.Add(api.ToEntity());
                }
                context.SaveChanges();
            }

            if (!context.IdentityResources.Any())
            {
                foreach (var id in Config.Ids)
                {
                    if (id.Name == "openid")
                    {
                        id.DisplayName = "用户标识符";
                    }
                    if (id.Name == "profile")
                    {
                        id.DisplayName = "用户账号信息";
                        id.Description = "你的账号信息(姓名等)";
                    }
                    context.IdentityResources.Add(id.ToEntity());
                }
                context.SaveChanges();
            }
        }
Exemple #17
0
 private void UpdateClientConfig(IS4Tenant tenant, List <int> serviceProviders)
 {
     foreach (var client in _configurationDbContext.Clients.AsQueryable().Where(c => serviceProviders.Contains(c.Id)).Include("RedirectUris").Include("PostLogoutRedirectUris"))
     {
         client.RedirectUris.Add(new ClientRedirectUri()
         {
             Client = client, RedirectUri = $"https://{tenant.Name}.localhost:{(client.Id == 1 ? "44372" : "44334")}/signin-oidc-{tenant.TenantId}"
         });
         client.PostLogoutRedirectUris.Add(new ClientPostLogoutRedirectUri()
         {
             Client = client, PostLogoutRedirectUri = $"https://{tenant.Name}.localhost:{(client.Id == 1 ? "44372" : "44334")}/signout-callback-oidc-{tenant.TenantId}"
         });
         _configurationDbContext.Clients.Attach(client).State = EntityState.Modified;
     }
     _configurationDbContext.SaveChanges();
 }
Exemple #18
0
        public void Seed(IConfigurationDbContext context)
        {
            //identity resources
            if (IdentityResources != null)
            {
                foreach (var item in IdentityResources)
                {
                    var existingIdentityResource = context.IdentityResources
                                                   .Include(_ => _.UserClaims)
                                                   .FirstOrDefault(_ => _.Name == item.Name);

                    if (existingIdentityResource == null)
                    {
                        context.IdentityResources.Add(item.ToEntity());
                    }
                    //else
                    //    Mapper.Map(item, existingIdentityResource);
                }
            }

            //api resources
            if (ApiResources != null)
            {
                foreach (var item in ApiResources)
                {
                    var existingApiResource = context.ApiResources.Include(_ => _.Scopes)
                                              .Include(_ => _.Secrets)
                                              .Include(_ => _.UserClaims)
                                              .FirstOrDefault(_ => _.Name == item.Name);
                    if (existingApiResource == null)
                    {
                        context.ApiResources.Add(item.ToEntity());
                    }
                    //else
                    //    Mapper.Map(item, existingApiResource);
                }
            }

            //clients
            if (Clients != null)
            {
                foreach (var item in Clients)
                {
                    foreach (var secret in item.ClientSecrets)
                    {
                        secret.Value = secret.Value.Sha256();
                    }
                    var existingClient = context.Clients.Include(_ => _.ClientSecrets)
                                         .Include(_ => _.AllowedCorsOrigins)
                                         .Include(_ => _.AllowedScopes)
                                         .Include(_ => _.Claims)
                                         .Include(_ => _.AllowedGrantTypes)
                                         .Include(_ => _.RedirectUris)
                                         .Include(_ => _.PostLogoutRedirectUris)
                                         .FirstOrDefault(c => c.ClientId == item.ClientId);
                    if (existingClient == null)
                    {
                        context.Clients.Add(item.ToEntity());
                    }
                    //else
                    //    Mapper.Map(item, existingClient);
                }
            }

            context.SaveChanges();
        }
Exemple #19
0
        private static void EnsureSeedData(IConfigurationDbContext context)
        {
            Console.WriteLine("Seeding database...");

            if (!context.Clients.Any())
            {
                Console.WriteLine("Clients being populated");
                foreach (var client in Clients.Get())
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Console.WriteLine("Clients already populated");
            }

            if (!context.IdentityResources.Any())
            {
                Console.WriteLine("IdentityResources being populated");
                foreach (var resource in Resources.IdentityResources)
                {
                    context.IdentityResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Console.WriteLine("IdentityResources already populated");
            }

            if (!context.ApiResources.Any())
            {
                Console.WriteLine("ApiResources being populated");
                foreach (var resource in Resources.ApiResources)
                {
                    context.ApiResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Console.WriteLine("ApiResources already populated");
            }

            if (!context.ApiScopes.Any())
            {
                Console.WriteLine("Scopes being populated");
                foreach (var resource in Resources.ApiScopes)
                {
                    context.ApiScopes.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Console.WriteLine("Scopes already populated");
            }

            Console.WriteLine("Done seeding database.");
            Console.WriteLine();
        }
        internal virtual void EnsureSeedData()
        {
            this._logger.LogDebug("Ensure Seed Data");

            string rootPath = _options.SeedExampleDataPath.GetFullPath(
                this._environment.ContentRootPath);

            if (!_configurationDbContext.IdentityResources.Any())
            {
                var path = Path.Combine(rootPath,
                                        "data_resources_identity.json");

                _logger.LogDebug($"Loading file: {path}");

                var resources = JsonConvert
                                .DeserializeObject <List <IdentityResource> >(
                    File.ReadAllText(path));

                foreach (var resource in resources)
                {
                    _configurationDbContext.IdentityResources
                    .Add(resource.ToEntity());
                }

                _configurationDbContext.SaveChanges();
                _logger.LogDebug("Saved Resource Identities");
            }

            if (!_configurationDbContext.ApiResources.Any())
            {
                var path = Path.Combine(rootPath, "data_resources_api.json");
                _logger.LogDebug($"Loading file: {path}");

                var resources = JsonConvert
                                .DeserializeObject <List <ApiResource> >(
                    File.ReadAllText(path));

                foreach (var resource in resources)
                {
                    _configurationDbContext.ApiResources
                    .Add(resource.ToEntity());
                }

                _configurationDbContext.SaveChanges();
                _logger.LogDebug("Saved Resource API");
            }

            if (!_configurationDbContext.Clients.Any())
            {
                var path = Path.Combine(rootPath, "data_clients.json");
                _logger.LogDebug($"Loading file: {path}");

                var clients = JsonConvert
                              .DeserializeObject <List <Client> >(File.ReadAllText(path));

                foreach (var client in clients)
                {
                    _configurationDbContext.Clients.Add(client.ToEntity());
                }
                _configurationDbContext.SaveChanges();
                _logger.LogDebug("Saved Clients");
            }

            if (!_userAccountDbContext.UserAccounts.Any())
            {
                var path = Path.Combine(rootPath, "data_users.json");
                _logger.LogDebug($"Loading file: {path}");

                var userAccounts = JsonConvert
                                   .DeserializeObject <List <UserAccount> >(
                    File.ReadAllText(path));

                foreach (var userAccount in userAccounts)
                {
                    _userAccountDbContext.UserAccounts
                    .Add(userAccount.ToEntity());
                }

                _userAccountDbContext.SaveChanges();
                _logger.LogDebug("Saved Users");
            }
        }
 public void Delete(Client entity)
 {
     _dbSet.Remove(entity);
     _dbContext.SaveChanges();
 }
 public void Save()
 {
     _db.SaveChanges();
 }
Exemple #23
0
        public int InsertClient(ClientApiModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var client = new Client()
            {
                ClientId   = model.ClientId,
                Enabled    = model.Enabled,
                ClientName = model.ClientName,
                // Needed to be able to obtain refresh token.
                AllowOfflineAccess           = true,
                AccessTokenLifetime          = model.AccessTokenLifetime,
                AbsoluteRefreshTokenLifetime = model.RefreshTokenLifetime
            };

            AddOrUpdateClientSecret(client, model.ClientSecret);
            AddOrUpdateClientRedirectUrl(client, model.RedirectUrl);

            client.AllowedGrantTypes = new List <ClientGrantType>()
            {
                new ClientGrantType()
                {
                    Client    = client,
                    GrantType = OidcConstants.GrantTypes.AuthorizationCode
                },
                new ClientGrantType()
                {
                    Client    = client,
                    GrantType = OidcConstants.GrantTypes.RefreshToken
                },
                new ClientGrantType()
                {
                    Client    = client,
                    GrantType = OidcConstants.GrantTypes.JwtBearer
                },
                new ClientGrantType()
                {
                    Client    = client,
                    GrantType = OidcConstants.GrantTypes.ClientCredentials
                }
            };

            client.AllowedScopes = new List <ClientScope>()
            {
                new ClientScope()
                {
                    Client = client,
                    Scope  = "nop_api"
                }
            };

            client.Claims = new List <ClientClaim>()
            {
                new ClientClaim()
                {
                    Client = client,
                    Type   = JwtClaimTypes.Subject,
                    Value  = client.ClientId
                },
                new ClientClaim()
                {
                    Client = client,
                    Type   = JwtClaimTypes.Name,
                    Value  = client.ClientName
                }
            };

            _configurationDbContext.Clients.Add(client);
            _configurationDbContext.SaveChanges();

            return(client.Id);
        }