コード例 #1
0
        public async Task <IActionResult> Edit(long id, TenantViewModel Tenant)
        {
            if (id != Tenant.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                ApplicationTenant applicationTenant = _context.Tenants.Find(Tenant.Id);
                try
                {
                    applicationTenant.Name         = Tenant.Name;
                    applicationTenant.EmailAddress = Tenant.EmailAddress;
                    applicationTenant.LogoURL      = Tenant.LogoURL;
                    applicationTenant.PhoneContact = Tenant.PhoneContact;
                    applicationTenant.System       = Tenant.System;
                    applicationTenant.Visible      = Tenant.Visible;
                    applicationTenant.WebSite      = Tenant.WebSite;
                    await userManager.UpdateTenantAsync(applicationTenant);
                }


                catch (DbUpdateConcurrencyException)
                {
                    if (!ApplicationTenantExists(applicationTenant.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(Tenant));
        }
コード例 #2
0
        public static void EnsureSeedData(IServiceProvider serviceProvider)
        {
            Console.WriteLine("Seeding database...");

            using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var context = scope.ServiceProvider.GetService <ManagementDbContext>();
                context.Database.Migrate();

                var tenantMgr = scope.ServiceProvider.GetRequiredService <TenantManager <ApplicationTenant, string> >();
                var tenant1   = tenantMgr.FindByCanonicalNameAsync("tenant1").Result;
                if (tenant1 == null)
                {
                    tenant1 = new ApplicationTenant
                    {
                        CanonicalName = "tenant1",
                        DisplayName   = "Tenant One",
                        DatabaseName  = "tenant1",
                    };
                    var result = tenantMgr.CreateAsync(tenant1).Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }

                    Console.WriteLine("tenant1 created");
                }
                else
                {
                    Console.WriteLine("tenant1 already exists");
                }

                var tenant2 = tenantMgr.FindByCanonicalNameAsync("tenant2").Result;
                if (tenant2 == null)
                {
                    tenant2 = new ApplicationTenant
                    {
                        CanonicalName = "tenant2",
                        DisplayName   = "Tenant Two",
                        DatabaseName  = "tenant2",
                    };
                    var result = tenantMgr.CreateAsync(tenant2).Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }

                    Console.WriteLine("tenant2 created");
                }
                else
                {
                    Console.WriteLine("tenant2 already exists");
                }
            }

            using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var tenantMgr      = scope.ServiceProvider.GetRequiredService <TenantManager <ApplicationTenant, string> >();
                var tenant1        = tenantMgr.FindByCanonicalNameAsync("Tenant1").Result;
                var tenancyContext = scope.ServiceProvider.GetService <ITenancyContext <ApplicationTenant, string> >();
                tenancyContext.Tenant = tenant1;

                var context = scope.ServiceProvider.GetService <ApplicationDbContext>();
                context.Database.Migrate();

                var userMgr = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
                var alice   = userMgr.FindByNameAsync("*****@*****.**").Result;
                if (alice == null)
                {
                    alice = new ApplicationUser
                    {
                        UserName = "******",
                        Email    = "*****@*****.**"
                    };
                    var result = userMgr.CreateAsync(alice, "Pass123$").Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }

                    Console.WriteLine("alice created");
                }
                else
                {
                    Console.WriteLine("alice already exists");
                }

                var bob = userMgr.FindByNameAsync("*****@*****.**").Result;
                if (bob == null)
                {
                    bob = new ApplicationUser
                    {
                        UserName = "******",
                        Email    = "*****@*****.**"
                    };
                    var result = userMgr.CreateAsync(bob, "Pass123$").Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }

                    Console.WriteLine("bob created");
                }
                else
                {
                    Console.WriteLine("bob already exists");
                }
            }

            using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var tenantMgr      = scope.ServiceProvider.GetRequiredService <TenantManager <ApplicationTenant, string> >();
                var tenant2        = tenantMgr.FindByCanonicalNameAsync("Tenant2").Result;
                var tenancyContext = scope.ServiceProvider.GetService <ITenancyContext <ApplicationTenant, string> >();
                tenancyContext.Tenant = tenant2;

                var context = scope.ServiceProvider.GetService <ApplicationDbContext>();
                context.Database.Migrate();

                var userMgr = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
                var alice   = userMgr.FindByNameAsync("*****@*****.**").Result;
                if (alice == null)
                {
                    alice = new ApplicationUser
                    {
                        UserName = "******",
                        Email    = "*****@*****.**"
                    };
                    var result = userMgr.CreateAsync(alice, "Pass123$").Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }

                    Console.WriteLine("alice created");
                }
                else
                {
                    Console.WriteLine("alice already exists");
                }

                var chris = userMgr.FindByNameAsync("*****@*****.**").Result;
                if (chris == null)
                {
                    chris = new ApplicationUser
                    {
                        UserName = "******",
                        Email    = "*****@*****.**"
                    };
                    var result = userMgr.CreateAsync(chris, "Pass123$").Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }

                    Console.WriteLine("chris created");
                }
                else
                {
                    Console.WriteLine("chris already exists");
                }
            }

            Console.WriteLine("Done seeding database.");
            Console.WriteLine();
        }
コード例 #3
0
 public Navigation(ApplicationTenant tenant)
 {
     _tenant = tenant;
 }
コード例 #4
0
        public static void EnsureSeedData(IServiceProvider serviceProvider)
        {
            Console.WriteLine("Seeding database...");

            using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var context = scope.ServiceProvider.GetService <ApplicationDbContext>();
                context.Database.Migrate();

                var tenantMgr = scope.ServiceProvider.GetRequiredService <TenantManager <ApplicationTenant> >();
                var tenant1   = tenantMgr.FindByCanonicalNameAsync("localhost").Result;
                if (tenant1 == null)
                {
                    tenant1 = new ApplicationTenant
                    {
                        CanonicalName = "localhost",
                        DisplayName   = "Tenant One",
                    };
                    var result = tenantMgr.CreateAsync(tenant1).Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }

                    Console.WriteLine("tenant1 created");
                }
                else
                {
                    Console.WriteLine("tenant1 already exists");
                }

                var tenant2 = tenantMgr.FindByCanonicalNameAsync("tenant2.local").Result;
                if (tenant2 == null)
                {
                    tenant2 = new ApplicationTenant
                    {
                        CanonicalName = "tenant2.local",
                        DisplayName   = "Tenant Two",
                    };
                    var result = tenantMgr.CreateAsync(tenant2).Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }

                    Console.WriteLine("tenant2 created");
                }
                else
                {
                    Console.WriteLine("tenant2 already exists");
                }
            }

            using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var tenantMgr      = scope.ServiceProvider.GetRequiredService <TenantManager <ApplicationTenant> >();
                var tenant         = tenantMgr.FindByCanonicalNameAsync("localhost").Result;
                var tenancyContext = scope.ServiceProvider.GetService <ITenancyContext <ApplicationTenant> >();
                tenancyContext.Tenant = tenant;

                var context = scope.ServiceProvider.GetService <ApplicationDbContext>();

                EnsureSeedData(context);

                var userMgr = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
                var alice   = userMgr.FindByNameAsync("alice").Result;
                if (alice == null)
                {
                    alice = new ApplicationUser
                    {
                        UserName = "******"
                    };
                    var result = userMgr.CreateAsync(alice, "Pass123$").Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }

                    result = userMgr.AddClaimsAsync(alice, new Claim[] {
                        new Claim(JwtClaimTypes.Name, "Alice Smith"),
                        new Claim(JwtClaimTypes.GivenName, "Alice"),
                        new Claim(JwtClaimTypes.FamilyName, "Smith"),
                        new Claim(JwtClaimTypes.Email, "*****@*****.**"),
                        new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),
                        new Claim(JwtClaimTypes.WebSite, "http://alice.com"),
                        new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServer4.IdentityServerConstants.ClaimValueTypes.Json)
                    }).Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }
                    Console.WriteLine("alice created");
                }
                else
                {
                    Console.WriteLine("alice already exists");
                }

                var bob = userMgr.FindByNameAsync("bob").Result;
                if (bob == null)
                {
                    bob = new ApplicationUser
                    {
                        UserName = "******"
                    };
                    var result = userMgr.CreateAsync(bob, "Pass123$").Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }

                    result = userMgr.AddClaimsAsync(bob, new Claim[] {
                        new Claim(JwtClaimTypes.Name, "Bob Smith"),
                        new Claim(JwtClaimTypes.GivenName, "Bob"),
                        new Claim(JwtClaimTypes.FamilyName, "Smith"),
                        new Claim(JwtClaimTypes.Email, "*****@*****.**"),
                        new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),
                        new Claim(JwtClaimTypes.WebSite, "http://bob.com"),
                        new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServer4.IdentityServerConstants.ClaimValueTypes.Json),
                        new Claim("location", "somewhere")
                    }).Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }
                    Console.WriteLine("bob created");
                }
                else
                {
                    Console.WriteLine("bob already exists");
                }
            }

            using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var tenantMgr      = scope.ServiceProvider.GetRequiredService <TenantManager <ApplicationTenant> >();
                var tenant         = tenantMgr.FindByCanonicalNameAsync("tenant2.local").Result;
                var tenancyContext = scope.ServiceProvider.GetService <ITenancyContext <ApplicationTenant> >();
                tenancyContext.Tenant = tenant;

                var context = scope.ServiceProvider.GetService <ApplicationDbContext>();

                EnsureSeedData(context);

                var userMgr = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
                var alice   = userMgr.FindByNameAsync("alice").Result;
                if (alice == null)
                {
                    alice = new ApplicationUser
                    {
                        UserName = "******"
                    };
                    var result = userMgr.CreateAsync(alice, "Pass123$").Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }

                    result = userMgr.AddClaimsAsync(alice, new Claim[] {
                        new Claim(JwtClaimTypes.Name, "Alice Smith"),
                        new Claim(JwtClaimTypes.GivenName, "Alice"),
                        new Claim(JwtClaimTypes.FamilyName, "Smith"),
                        new Claim(JwtClaimTypes.Email, "*****@*****.**"),
                        new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),
                        new Claim(JwtClaimTypes.WebSite, "http://alice.com"),
                        new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServer4.IdentityServerConstants.ClaimValueTypes.Json)
                    }).Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }
                    Console.WriteLine("alice created");
                }
                else
                {
                    Console.WriteLine("alice already exists");
                }

                var chris = userMgr.FindByNameAsync("chris").Result;
                if (chris == null)
                {
                    chris = new ApplicationUser
                    {
                        UserName = "******"
                    };
                    var result = userMgr.CreateAsync(chris, "Pass123$").Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }

                    result = userMgr.AddClaimsAsync(chris, new Claim[] {
                        new Claim(JwtClaimTypes.Name, "Chris Smith"),
                        new Claim(JwtClaimTypes.GivenName, "Chris"),
                        new Claim(JwtClaimTypes.FamilyName, "Smith"),
                        new Claim(JwtClaimTypes.Email, "*****@*****.**"),
                        new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),
                        new Claim(JwtClaimTypes.WebSite, "http://chris.com"),
                        new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServer4.IdentityServerConstants.ClaimValueTypes.Json),
                        new Claim("location", "somewhere")
                    }).Result;
                    if (!result.Succeeded)
                    {
                        throw new Exception(result.Errors.First().Description);
                    }
                    Console.WriteLine("chris created");
                }
                else
                {
                    Console.WriteLine("chris already exists");
                }
            }

            Console.WriteLine("Done seeding database.");
            Console.WriteLine();
        }
コード例 #5
0
 public SendGridEmailSender(IConfiguration configuration, ApplicationTenant tenant)
 {
     _configuration = configuration;
     _tenant        = tenant;
 }
コード例 #6
0
 public ApplicationRequest(ApplicationTenant applicationTenant)
 {
     ApplicationTenant = applicationTenant;
     RequestId         = Guid.NewGuid();
 }
コード例 #7
0
 public ThemeStyle(ApplicationTenant tenant)
 {
     _tenant = tenant;
 }
コード例 #8
0
        public static async Task Initialize(ApplicationDbContext context,
                                            AppUserManager userManager,
                                            RoleManager <ApplicationRole> roleManager, AppSetupOptions options, ILogger logger)
        {
            logger.LogInformation("Data Seeder Started");
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (userManager is null)
            {
                throw new ArgumentNullException(nameof(userManager));
            }

            if (roleManager is null)
            {
                throw new ArgumentNullException(nameof(roleManager));
            }

            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (logger is null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            context.Database.EnsureCreated();
            try
            {
                if (await roleManager.FindByNameAsync(options.AdminRoleName) == null)
                {
                    await roleManager.CreateAsync(new ApplicationRole { Name = options.AdminRoleName });

                    var adminRole = await roleManager.FindByNameAsync(options.AdminRoleName);

                    await roleManager.AddClaimAsync(adminRole, new Claim(CustomClaimTypes.Permission, Permissions.AdminUser.Read));
                }

                if (await roleManager.FindByNameAsync(options.ManagerRoleName) == null)
                {
                    await roleManager.CreateAsync(new ApplicationRole { Name = options.ManagerRoleName });
                }

                if (await roleManager.FindByNameAsync(options.NormalRoleName) == null)
                {
                    await roleManager.CreateAsync(new ApplicationRole { Name = options.NormalRoleName });
                }



                ApplicationUser user = await userManager.FindByNameAsync(options.AdminUserName);

                if (user == null)
                {
                    user                = new ApplicationUser();
                    user.UserName       = options.AdminUserName;
                    user.EmailConfirmed = true;
                    user.Email          = options.AdminUserName;
                    var result = await userManager.CreateAsync(user, options.AdminUserPass);

                    var tenantresult = await userManager.CreateTenantAsync(options.DefaultTenantName, user);

                    if (result.Succeeded && tenantresult.Succeeded)
                    {
                        ApplicationTenant tenant = userManager.FindTenantByName(options.DefaultTenantName);
                        await userManager.AddToRoleAsync(user, tenant, options.AdminRoleName);
                    }
                }
                List <GeoCity> listOfLocations         = new List <GeoCity>();
                IOrderedEnumerable <Country> countries = null;
                if (!context.Countries.Any())
                {
                    listOfLocations.AddRange(File.ReadLines("Data\\worldcities.csv").Select(line => new GeoCity(line)).ToList());
                    countries = listOfLocations.Select(s => new Country {
                        Name = s.Country, CountryCode = s.Iso2
                    }).GroupBy(g => g.Name).Select(q => q.First()).OrderBy(o => o.Name);

                    var i = countries.Count();

                    await context.Countries.AddRangeAsync(countries);

                    await context.SaveChangesAsync();
                }

                if (!context.GeoLocations.Any() || countries == null)
                {
                    foreach (var location in listOfLocations)
                    {
                        GeoLocation Location = new GeoLocation();
                        Location.isCity    = true;
                        Location.Name      = location.City;
                        Location.Latitude  = location.Lat;
                        Location.Longitude = location.Lng;
                        Location.Country   = countries.FirstOrDefault(q => q.CountryCode == location.Iso2);
                        await context.GeoLocations.AddAsync(Location);
                    }
                }


                await context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "An error occurred while seeding the database.");
                //throw;
            }
        }
コード例 #9
0
        public void WhenCeateUserAsyncWithTenant()
        {
            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(_session));
            var user = new ApplicationUser() { UserName = "******" };
            var tenant = new ApplicationTenant() { Name = "AnyCompany_LTDA" };

            //tenant.Users.Add(user);
            user.Tenant = tenant;

            using (var transaction = new TransactionScope())
            {
                var result = userManager.CreateAsync(user, "RealPassword").GetAwaiter().GetResult();
                transaction.Complete();
                Assert.AreEqual(0, result.Errors.Count());
            }

            var actual = _session.Query<ApplicationUser>().FirstOrDefault(x => x.UserName == user.UserName);

            Assert.IsNotNull(actual);
            Assert.AreEqual(user.UserName, actual.UserName);
            Assert.AreEqual(user.Tenant, actual.Tenant);
        }
コード例 #10
0
 public TenantClaimsRepository(ApplicationTenant tenant)
     : base(tenant.Database.ConnectionString)
 {
 }