コード例 #1
0
        public async Task <IActionResult> Create(
            [Bind(
                 nameof(CreateTenantModel.ParentTenantId),
                 nameof(CreateTenantModel.ConfirmPassword),
                 nameof(CreateTenantModel.Email),
                 nameof(CreateTenantModel.LoginDomain),
                 nameof(CreateTenantModel.Password),
                 nameof(CreateTenantModel.TenantName)
                 )]
            CreateTenantModel model)
        {
            if (ModelState.IsValid)
            {
                var t = new Tenant
                {
                    ParentTenantId = model.ParentTenantId.GetValueOrDefault(TenantId),
                    LoginDomain    = model.LoginDomain,
                    TenantName     = model.TenantName
                };
                Rdb.Tenants.Add(t);
                Rdb.Apps.Add(new App
                {
                    Tenant  = t,
                    AppType = AppTypes.Portal,
                    AppName = "Portal"
                });
                await Rdb.SaveChangesAsync();

                var role = ApplicationRole.CreateConfigurationMasterRole(t.TenantId);
                Rdb.Roles.Add(role);
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, TenantId = t.TenantId, EmailConfirmed = true
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                await Rdb.SaveChangesAsync();

                Rdb.UserRoles.Add(new ApplicationUserRole
                {
                    RoleId = role.Id,
                    UserId = user.Id
                });
                await Rdb.SaveChangesAsync();

                SetToast(AspHelpers.ToastMessages.Saved);
                return(RedirectToAction(ActionNames.TenantsList));
            }
            return(View(ViewNames.TenantEdit, model));
        }