Ejemplo n.º 1
0
        public void Init()
        {
            var connectionString = ConfigurationHelper.GetConnectionString(true);

            var services = new ServiceCollection();

            services.AddTransient(typeof(IMultiTenantUserClaimStore <User, long>), typeof(MultiTenantUserClaimStore <User, Role, long, long>));
            services.AddTransient(typeof(IMultiTenantUserRoleStore <User, long>), typeof(MultiTenantUserRoleStore <User, Role, long, long>));

            var builder = services.AddIdentity <User, Role>()
                          .AddEntityFrameworkStores <MultiTenantIamDbContext <User, Role, long, long> >()
                          .AddDefaultTokenProviders();

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = "Bearer";
                options.DefaultChallengeScheme    = "Bearer";
            });

            services.AddAuthorization();

            services.AddMultiTenantIamCore <long>();

            services.AddDbContext <MultiTenantIamDbContext <User, Role, long, long> >(options =>
                                                                                      options.UseSqlServer(connectionString));

            serviceProvider = services.BuildServiceProvider();

            using (var scope = serviceProvider.CreateScope())
            {
                var dbContext = scope.ServiceProvider.GetRequiredService(typeof(MultiTenantIamDbContext <User, Role, long, long>)) as MultiTenantIamDbContext <User, Role, long, long>;

                dbContext.Database.EnsureCreated();

                new Checkpoint().Reset(connectionString).Wait();
            }

            userManager = serviceProvider.GetRequiredService(typeof(UserManager <User>)) as UserManager <User>;
            claimStore  = serviceProvider.GetRequiredService(typeof(IMultiTenantUserClaimStore <User, long>)) as IMultiTenantUserClaimStore <User, long>;
            roleStore   = serviceProvider.GetRequiredService(typeof(IMultiTenantUserRoleStore <User, long>)) as IMultiTenantUserRoleStore <User, long>;

            var roleManager = serviceProvider.GetRequiredService(typeof(RoleManager <Role>)) as RoleManager <Role>;

            roleManager.CreateAsync(new Role()
            {
                Name = "admin"
            }).Wait();

            roleManager.CreateAsync(new Role()
            {
                Name = "manager"
            }).Wait();

            userManager.CreateAsync(new User()
            {
                UserName = "******",
            }).Wait();

            user = userManager.FindByNameAsync("test").Result;
        }
        public void Init()
        {
            var connectionString = ConfigurationHelper.GetConnectionString();

            var services = new ServiceCollection();

            var builder = services.AddIdentity <User, Role>()
                          .AddEntityFrameworkStores <IamDbContext <User, Role, long> >()
                          .AddDefaultTokenProviders();

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = "Bearer";
                options.DefaultChallengeScheme    = "Bearer";
            });

            services.AddAuthorization();

            services.AddIamCore();

            services.AddDbContext <IamDbContext <User, Role, long> >(options =>
                                                                     options.UseSqlServer(connectionString));

            serviceProvider = services.BuildServiceProvider();

            using (var scope = serviceProvider.CreateScope())
            {
                var dbContext = scope.ServiceProvider.GetRequiredService(typeof(IamDbContext <User, Role, long>)) as IamDbContext <User, Role, long>;

                dbContext.Database.EnsureCreated();

                new Checkpoint().Reset(connectionString).Wait();
            }

            roleManager = serviceProvider.GetRequiredService(typeof(RoleManager <Role>)) as RoleManager <Role>;

            roleManager.CreateAsync(new Role()
            {
                Name = "test",
            }).Wait();

            role = roleManager.FindByNameAsync("test").Result;
        }
        protected IntegrationTestBase()
        {
            _server = new Microsoft.AspNetCore.TestHost.TestServer(new WebHostBuilder()
                                                                   .UseStartup <IdentityFramework.Iam.TestServer.Startup>()
                                                                   .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                config.AddEnvironmentVariables();
                config.AddInMemoryCollection(new Dictionary <string, string>()
                {
                    { "UseMultitenancy", "false" },
                    { "TestMode", "true" }
                });
            })
                                                                   .ConfigureTestServices(services =>
            {
                services.AddIdentity <User, Role>()
                .AddEntityFrameworkStores <IamDbContext <User, Role, long> >()
                .AddDefaultTokenProviders();

                services.AddAuthentication(options =>
                {
                    options.DefaultAuthenticateScheme = "Bearer";
                    options.DefaultChallengeScheme    = "Bearer";
                }).AddJwtBearer(configureOptions =>
                {
                    configureOptions.ClaimsIssuer = Startup.TokenValidationParameters.ValidIssuer;
                    configureOptions.TokenValidationParameters = Startup.TokenValidationParameters;
                    configureOptions.SaveToken = true;
                });

                services.AddAuthorization();

                services.AddMvc();

                services.AddIamEntityFramework <User, Role, long>(options =>
                                                                  options.UseSqlServer(ConfigurationHelper.GetConnectionString()));
            }));

            IdentityFramework.Iam.TestServer.Program.SeedData(_server.Host.Services, typeof(IamDbContext <User, Role, long>), ConfigurationHelper.GetConnectionString());
        }