예제 #1
0
 private void SeedClients()
 {
     if (!_configDbContext.Clients.Any())
     {
         Console.WriteLine("Clients being populated");
         var clients = _mapper.Map <IEnumerable <Client> >(_identitySettings.Clients);
         foreach (var client in clients)
         {
             _configDbContext.Clients.Add(client.ToEntity());
         }
         _configDbContext.SaveChanges();
     }
     else
     {
         Console.WriteLine("Clients already populated");
     }
 }
예제 #2
0
        private static void EnsureSeedData(ConfigurationDbContext context)
        {
            if (!context.Clients.Any())
            {
                Console.WriteLine("Clients being populated");
                foreach (var client in Config.GetClients().ToList())
                {
                    Console.Write(client);
                    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");
            }
        }
        private static void Migrate(ServiceProvider sp, ConfigurationDbContext configurationContext, ApplicationDbContext identityContext)
        {
            if (!configurationContext.IdentityResources.AnyAsync().Result)
            {
                configurationContext.IdentityResources.Add(new IdentityResources.OpenId().ToEntity());
                configurationContext.IdentityResources.Add(new IdentityResources.Email().ToEntity());
                configurationContext.IdentityResources.Add(new IdentityResources.Profile().ToEntity());

                configurationContext.SaveChanges();
            }

            if (!configurationContext.ApiResources.AnyAsync().Result)
            {
                var api = new ApiResource("myAppoiments_api", "Web Api call")
                {
                    ApiSecrets = { new Secret("secret".Sha256()) },
                    Scopes     =
                    {
                        "myAppoiments_api.full",
                        "myAppoiments_api.read",
                        "myAppoiments_api"
                    },
                    UserClaims =
                    {
                        ClaimTypes.NameIdentifier,
                        ClaimTypes.Name,
                        ClaimTypes.Email,
                        ClaimTypes.Role,
                        JwtClaimTypes.Role,
                        JwtClaimTypes.Name,
                        JwtClaimTypes.Email
                    }
                };

                //adding local API
                var localApi = new ApiResource(IdentityServerConstants.LocalApi.ScopeName);

                configurationContext.ApiResources.Add(api.ToEntity());
                configurationContext.ApiResources.Add(localApi.ToEntity());

                configurationContext.SaveChanges();
            }

            if (!configurationContext.ApiScopes.AnyAsync().Result)
            {
                var api = new ApiScope("myAppoiments_api", "Web Api call")
                {
                    UserClaims =
                    {
                        ClaimTypes.NameIdentifier,
                        ClaimTypes.Name,
                        ClaimTypes.Email,
                        ClaimTypes.Role,
                        JwtClaimTypes.Role,
                        JwtClaimTypes.Name,
                        JwtClaimTypes.Email
                    }
                };

                var localApi = new ApiScope(IdentityServerConstants.LocalApi.ScopeName);

                configurationContext.ApiScopes.Add(api.ToEntity());
                configurationContext.ApiScopes.Add(localApi.ToEntity());

                configurationContext.SaveChanges();
            }

            if (!configurationContext.Clients.AnyAsync().Result)
            {
                List <Client> clients = new List <Client>()
                {
                    new Client
                    {
                        ClientId      = "mvc",
                        ClientSecrets = { new Secret("secret".Sha256()) },

                        AllowedGrantTypes = GrantTypes.Code,

                        // where to redirect to after login
                        RedirectUris = { "http://*****:*****@myappoiments.com",
                    UserType    = UserType.CompanyUser
                }, "1qaz!QAZ").Wait();

                var user = userManager.Users.FirstOrDefaultAsync(x => x.UserName == "*****@*****.**").Result;

                userManager.AddToRoleAsync(user, Role.CompanyUser).Wait();

                userManager.CreateAsync(new ApplicationUser()
                {
                    DisplayName = "Public_user 1",
                    UserName    = "******",
                    Email       = "*****@*****.**",
                    UserType    = UserType.PublicUser
                }, "1qaz!QAZ").Wait();

                user = userManager.Users.FirstOrDefaultAsync(x => x.UserName == "*****@*****.**").Result;

                userManager.AddToRoleAsync(user, Role.PublicUser).Wait();
            }
        }
        private static void Migrate(ServiceProvider sp, ConfigurationDbContext configurationContext, ApplicationDbContext identityContext)
        {
            if (!configurationContext.IdentityResources.AnyAsync().Result)
            {
                configurationContext.IdentityResources.Add(new IdentityResources.OpenId().ToEntity());
                configurationContext.IdentityResources.Add(new IdentityResources.Email().ToEntity());
                configurationContext.IdentityResources.Add(new IdentityResources.Profile().ToEntity());

                configurationContext.SaveChanges();
            }

            if (!configurationContext.ApiResources.AnyAsync().Result)
            {
                var api = new ApiResource("i_api", "Web Api call")
                {
                    ApiSecrets = { new Secret("secret".Sha256()) },
                    Scopes     =
                    {
                        "i_api.full",
                        "i_api.read"
                    },
                    UserClaims =
                    {
                        ClaimTypes.NameIdentifier,
                        ClaimTypes.Name,
                        ClaimTypes.Email,
                        ClaimTypes.Role,
                        JwtClaimTypes.Role,
                        JwtClaimTypes.Name,
                        JwtClaimTypes.Email
                    }
                };
                configurationContext.ApiResources.Add(api.ToEntity());

                configurationContext.SaveChanges();
            }

            if (!configurationContext.Clients.AnyAsync().Result)
            {
                List <Client> clients = new List <Client>()
                {
                    new Client
                    {
                        ClientId      = "mvc",
                        ClientSecrets = { new Secret("secret".Sha256()) },

                        AllowedGrantTypes = GrantTypes.Code,

                        // where to redirect to after login
                        RedirectUris = { "http://localhost:52484/signin-oidc" },

                        // where to redirect to after logout
                        PostLogoutRedirectUris = { "http://localhost:52484/signout-callback-oidc" },

                        AllowedScopes = new List <string>
                        {
                            IdentityServerConstants.StandardScopes.OpenId,
                            IdentityServerConstants.StandardScopes.Profile,
                            IdentityServerConstants.StandardScopes.Email,
                            "i_api"
                        }
                    }
                };

                foreach (var client in clients)
                {
                    configurationContext.Clients.Add(client.ToEntity());
                }

                configurationContext.SaveChanges();
            }
        }