Example #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            string certFile = $"{_environment.ContentRootPath}{Path.DirectorySeparatorChar}Certificates{Path.DirectorySeparatorChar}nanofabrictest.pfx";
            var    cert     = new X509Certificate2(certFile, "idsrv3test");

            services.AddCors();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);


            // TODO:IdentiyServer Config Add
            IdentityRegistrar.Register(services);

            services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryIdentityResources(InMemoryConfiguration.IdentityResources())
            .AddInMemoryApiResources(InMemoryConfiguration.ApiResources())
            .AddInMemoryClients(InMemoryConfiguration.Clients())
            .AddAbpPersistedGrants <IAbpPersistedGrantDbContext>()
            .AddAbpIdentityServer <User>()
            .AddResourceOwnerValidator <ResourceOwnerPasswordValidator>()
            .AddProfileService <ProfileService>();


            // Configure Abp and Dependency Injection
            return(services.AddAbp <IdentityServerModule>(
                       // Configure Log4Net logging
                       options => options.IocManager.IocContainer.AddFacility <LoggingFacility>(
                           f => f.UseAbpLog4Net().WithConfig("log4net.config")
                           )
                       ));
        }
        private static void SeedData(IServiceScope serviceScope)
        {
            var context = serviceScope
                          .ServiceProvider
                          .GetRequiredService <ConfigurationDbContext>();

            if (!context.Clients.Any())
            {
                foreach (var client in InMemoryConfiguration.Clients())
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }

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

            if (!context.ApiResources.Any())
            {
                foreach (var resource in InMemoryConfiguration.ApiResources())
                {
                    context.ApiResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
        }
Example #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryClients(InMemoryConfiguration.Clients())
            .AddInMemoryIdentityResources(InMemoryConfiguration.IdentityResources())
            .AddInMemoryApiResources(InMemoryConfiguration.ApiResources());

            services.AddMvc();
        }
Example #4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentityServer()
            .AddSigningCredential(new X509Certificate2(@"C:\Users\Douglas\Documents\MisRepos\SocialNetwork\SocialNetwork.OAuth\socialnetwork.pfx", "pass123"))
            .AddTestUsers(InMemoryConfiguration.Users().ToList())
            .AddInMemoryClients(InMemoryConfiguration.Clients())
            .AddInMemoryIdentityResources(InMemoryConfiguration.IdentityResources())
            .AddInMemoryApiResources(InMemoryConfiguration.ApiResources());

            services.AddMvc();
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            string pfxFilePath = _configuration.GetValue <string>("SignInCredentials:PFXFile");
            string pfxFilePass = _configuration.GetValue <string>("SignInCredentials:Password");

            services.AddIdentityServer()
            .AddSigningCredential(new X509Certificate2(pfxFilePath, pfxFilePass))
            .AddTestUsers(InMemoryConfiguration.TestUsers().ToList())
            .AddInMemoryClients(InMemoryConfiguration.Clients())
            .AddInMemoryApiResources(InMemoryConfiguration.ApiResources())
            .AddInMemoryIdentityResources(InMemoryConfiguration.IdentityResources());
        }
Example #6
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentityServer()
            .AddSigningCredential(new X509Certificate2(
                                      @"D:\AspMVC\Identity-Server4\SocialNetwork.OAuth\SocialNetwork.OAuth\socialnetwork.pfx",
                                      "password123")) // to use our own certificate
            //.AddDeveloperSigningCredential()  //AddTemporarySigningCredential
            .AddTestUsers(InMemoryConfiguration.Users().ToList())
            .AddInMemoryClients(InMemoryConfiguration.Clients())
            .AddInMemoryIdentityResources(InMemoryConfiguration.IdentityResources())
            .AddInMemoryApiResources(InMemoryConfiguration.ApiResources());


            services.AddMvc();
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            //Specify How Identity Server Works
            services.AddIdentityServer()
            //Need to Specify 3 Things?
            //1. Which Api Can use this Authorization Server
            //2. Which Client Authozired to use ID4
            //3. Which Users Authorized to Use ID4
            .AddSigningCredential(new X509Certificate2(@"C:\Users\mick_\Downloads\IdentityServer4.Core2.ImplicitFlow.Token-003_Implicit_Flow_Token\IdentityServer4.Core2.ImplicitFlow.Token-003_Implicit_Flow_Token\identityserver.pfx", "Wind123456"))
            .AddTestUsers(InMemoryConfiguration.Users().ToList())
            .AddInMemoryClients(InMemoryConfiguration.Clients())
            .AddInMemoryIdentityResources(InMemoryConfiguration.IdentityResources())
            .AddInMemoryApiResources(InMemoryConfiguration.ApiResources());

            services.AddMvc();
        }
Example #8
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();

            //var assembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
            services.AddIdentityServer()
            .AddSigningCredential(new X509Certificate2(@"D:\RnD\IAP\Security\IdentityProvider\src\EcoSystem\keys\ecosystem.pfx", "password"))
            .AddTestUsers(InMemoryConfiguration.Users().ToList())
            //.AddConfigurationStore(builder => builder.UseSqlServer(Configuration.GetConnectionString("EcoSystem.OAuth"), options => options.MigrationsAssembly(assembly)))
            //.AddOperationalStore(builder => builder.UseSqlServer(Configuration.GetConnectionString("EcoSystem.OAuth"), options => options.MigrationsAssembly(assembly)));
            //.AddTestUsers(IdentityServer4.Quickstart.UI.TestUsers.Users)
            .AddInMemoryClients(InMemoryConfiguration.Clients())
            .AddInMemoryIdentityResources(InMemoryConfiguration.IdentityResources())
            .AddInMemoryApiResources(InMemoryConfiguration.ApiResources());

            services.AddMvc();
        }
        private static void EnsureSeedData(ConfigurationDbContext context)
        {
            if (!context.Clients.Any())
            {
                Console.WriteLine("Clients being populated");
                foreach (var client in InMemoryConfiguration.Clients().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 InMemoryConfiguration.IdentityResources().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 InMemoryConfiguration.ApiResources().ToList())
                {
                    context.ApiResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
            else
            {
                Console.WriteLine("ApiResources already populated");
            }
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryIdentityResources(InMemoryConfiguration.IdentityResources())
            .AddInMemoryApiResources(InMemoryConfiguration.ApiResources())
            .AddInMemoryClients(InMemoryConfiguration.Clients())
            .AddTestUsers(InMemoryConfiguration.Users().ToList());

            services.AddCors(options => {
                options.AddPolicy("CorsPolicy", policy =>
                {
                    policy.AllowAnyOrigin();
                    policy.AllowAnyMethod();
                    policy.AllowAnyHeader();
                });
            });

            services.AddMvc(services => services.EnableEndpointRouting = false);
        }
Example #11
0
        public void MigrateInMemoryDataToSqlServer(IApplicationBuilder app)
        {
            using (var scope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
            {
                scope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.Migrate();

                var context = scope.ServiceProvider.GetRequiredService <ConfigurationDbContext>();

                context.Database.Migrate();

                if (!context.Clients.Any())
                {
                    foreach (var client in InMemoryConfiguration.Clients())
                    {
                        context.Clients.Add(client.ToEntity());
                    }

                    context.SaveChanges();
                }

                if (!context.IdentityResources.Any())
                {
                    foreach (var resource in InMemoryConfiguration.IdentityResources())
                    {
                        context.IdentityResources.Add(resource.ToEntity());
                    }

                    context.SaveChanges();
                }

                if (!context.ApiResources.Any())
                {
                    foreach (var resource in InMemoryConfiguration.ApiResources())
                    {
                        context.ApiResources.Add(resource.ToEntity());
                    }

                    context.SaveChanges();
                }
            }
        }
Example #12
0
        /// <summary>
        /// InitializeIdentityServerDatabase
        /// </summary>
        /// <param name="app">IApplicationBuilder</param>
        /// <returns></returns>
        private async Task InitializeDatabase(IApplicationBuilder app)
        {
            using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
            {
                var context = serviceScope.ServiceProvider.GetService <IConfigurationDbContext>();
                if (!context.Clients.Any())
                {
                    foreach (var client in InMemoryConfiguration.Clients())
                    {
                        await context.AddClient(client.ToEntity());
                    }
                }
                if (!context.ApiResources.Any())
                {
                    foreach (var resource in InMemoryConfiguration.ApiResources())
                    {
                        await context.AddApiResource(resource.ToEntity());
                    }
                }
                if (!context.IdentityResources.Any())
                {
                    foreach (var identity in InMemoryConfiguration.IdentityResources())
                    {
                        await context.AddIdentityResource(identity.ToEntity());
                    }
                }

                var repo = serviceScope.ServiceProvider.GetService <IUserRepository>();
                if (!repo.FindAll().Any())
                {
                    var user = new User
                    {
                        UserName = "******",
                        Password = "******",
                    };
                    repo.Insert(user);
                }
            }
        }
Example #13
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            // Add application services.
            services.AddTransient <IEmailSender, EmailSender>();

            services.AddMvc();

            services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryPersistedGrants()
            .AddInMemoryIdentityResources(InMemoryConfiguration.IdentityResources())
            .AddInMemoryApiResources(InMemoryConfiguration.ApiResources())
            .AddInMemoryClients(InMemoryConfiguration.Clients())
            //.AddAspNetIdentity<ApplicationUser>();
            .AddTestUsers(Users.All());
        }
Example #14
0
        public void MigrateInMemoryDataToSqlServer(IApplicationBuilder app)
        {
            using (var scope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
            {
                scope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.Migrate();
                var context = scope.ServiceProvider.GetRequiredService <ConfigurationDbContext>();
                context.Database.Migrate();

                if (!context.Clients.Any())
                {
                    foreach (var client in InMemoryConfiguration.Clients())
                    {
                        context.Clients.Add(client.ToEntity());
                    }

                    context.SaveChanges();
                }

                if (!context.IdentityResources.Any())
                {
                    foreach (var resource in InMemoryConfiguration.IdentityResources())
                    {
                        context.IdentityResources.Add(resource.ToEntity());
                    }

                    context.SaveChanges();
                }

                if (!context.ApiResources.Any())
                {
                    foreach (var resource in InMemoryConfiguration.ApiResources())
                    {
                        context.ApiResources.Add(resource.ToEntity());
                    }

                    context.SaveChanges();
                }

                if (!context.ApiScopes.Any())
                {
                    foreach (var apiScope in InMemoryConfiguration.ApiScopes())
                    {
                        context.ApiScopes.Add(apiScope.ToEntity());
                    }

                    context.SaveChanges();
                }

                var applicationContext = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
                applicationContext.Database.Migrate();

                if (!applicationContext.Users.Any())
                {
                    foreach (var user in InMemoryConfiguration.Users())
                    {
                        var passwordHasher = new PasswordHasher <ApplicationUser>();
                        var appUser        = new ApplicationUser
                        {
                            UserName           = "******",
                            NormalizedUserName = "******",
                            CustomElement      = "custom element"
                        };

                        appUser.PasswordHash = passwordHasher.HashPassword(appUser, "Test123!");
                        applicationContext.Users.Add(appUser);
                    }

                    applicationContext.SaveChanges();
                }
            }
        }
Example #15
0
        public void MigrateInMemoryDataToSqlServer(IApplicationBuilder app)
        {
            using var scope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope();
            scope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.Migrate();

            var context = scope.ServiceProvider.GetRequiredService <ConfigurationDbContext>();

            context.Database.Migrate();

            if (!context.Clients.Any())
            {
                foreach (var client in InMemoryConfiguration.Clients())
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }

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

            if (!context.ApiResources.Any())
            {
                foreach (var resource in InMemoryConfiguration.ApiResources())
                {
                    context.ApiResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }

            if (!context.ApiScopes.Any())
            {
                foreach (var apiScope in InMemoryConfiguration.ApiScopes())
                {
                    context.ApiScopes.Add(apiScope.ToEntity());
                }
                context.SaveChanges();
            }

            var userManager = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();

            foreach (var user in InMemoryConfiguration.Users())
            {
                var appUser = userManager.FindByNameAsync(user.Username).Result;
                if (appUser == null)
                {
                    appUser = new ApplicationUser
                    {
                        UserName       = user.Username,
                        Email          = user.Claims.Single(c => c.Type == "email").Value,
                        EmailConfirmed = true
                    };
                    var result = userManager.CreateAsync(appUser, user.Password).Result;
                    result = userManager.AddClaimsAsync(appUser, new Claim[] {
                        new Claim(JwtClaimTypes.Name, "Nils Gruson"),
                        new Claim(JwtClaimTypes.GivenName, "Nils"),
                        new Claim(JwtClaimTypes.FamilyName, "Gruson")
                    }).Result;
                }
                ;
            }
        }