public async Task <IActionResult> SignUp(SignUpForm signUp) { if (HttpContext.Items["domain"] as string == "default") { if (!string.IsNullOrEmpty(signUp.TenantDomain) && !string.IsNullOrEmpty(signUp.TenantName) && ModelState.IsValid) { if (Regex.Matches(signUp.TenantDomain, @"[a-zA-Z]").Count == signUp.TenantDomain.Length) { var blacklist = new string[] { "default", "admin", "NewTenantUser", "sa", "helper", "dbo", "guest", "sys", "ttask" }; if (!_tt.TenantAlreadyExists(signUp.TenantDomain) && !blacklist.Contains(signUp.TenantDomain)) { _newTenant.NewTenant(signUp.TenantName, signUp.TenantDomain); Directory.CreateDirectory(@"wwwroot/img/profile/" + signUp.TenantDomain); HttpContext.Items["domain"] = signUp.TenantDomain; int tenantId = _tt.GetTenantId(signUp.TenantDomain); var user = new User() { Id = 1, UserName = signUp.Email, Email = signUp.Email, FirstName = signUp.FirstName, Surname = signUp.Surname, PhoneNumber = signUp.PhoneNumber, IdTenant = tenantId }; HttpContext.Items["domain"] = signUp.TenantDomain; IdentityResult result = await UserMgr.CreateAsync(user, signUp.Password); if (result.Succeeded) { await UserMgr.AddToRolesAsync(user, new List <string> { nameof(Roles.NormalUser), nameof(Roles.DomainAdmin), nameof(Roles.Manager) }); var settings = new UserSettings() { Coloring = "0", CustomizeView = "0", Notifications = "0", IdUser = user.Id }; _ut.InsertSettings(settings); return(Redirect($"https://{HttpContext.Request.Host}/{signUp.TenantDomain}")); } } else { ModelState.AddModelError("CustomErr", $"Domain {signUp.TenantDomain} already exists."); } } else { ModelState.AddModelError("CustomErr", $"Domain has to contain letters only."); } } else { if (string.IsNullOrEmpty(signUp.TenantName)) { ModelState.AddModelError("signUp.TenantName", "Company name is required field!"); } if (string.IsNullOrEmpty(signUp.TenantDomain)) { ModelState.AddModelError("signUp.TenantDomain", "Domain is required field!"); } } } else { var idService = _serviceOrderTable.GetInUseServiceIdByTenantId(_tt.GetTenantId(HttpContext.Items["domain"] as string)); var noUsers = _ut.GetNOUsers(); if ((idService == (int)Services.Basic && noUsers < 5) || (idService == (int)Services.Pro && noUsers < 10) || idService == (int)Services.Business) { if (ModelState.IsValid) { signUp.TenantDomain = HttpContext.Items["domain"] as string; int tenantId = _tt.GetTenantId(signUp.TenantDomain); int userId = _ut.GetMaxId(); User user = await UserMgr.FindByEmailAsync(signUp.Email); if (user == null) { user = new User() { Id = userId, UserName = signUp.Email, Email = signUp.Email, FirstName = signUp.FirstName, Surname = signUp.Surname, PhoneNumber = signUp.PhoneNumber, IdTenant = tenantId }; IdentityResult result = await UserMgr.CreateAsync(user, signUp.Password); if (result.Succeeded) { await UserMgr.AddToRoleAsync(user, nameof(Roles.NormalUser)); if (userId == 1) { await UserMgr.AddToRoleAsync(user, nameof(Roles.DomainAdmin)); await UserMgr.AddToRoleAsync(user, nameof(Roles.Manager)); } var settings = new UserSettings() { Coloring = "0", CustomizeView = "0", Notifications = "0", IdUser = user.Id }; _ut.InsertSettings(settings); var TenantPolicyResult = await _authorization.AuthorizeAsync(User, "TenantPolicy"); if (TenantPolicyResult.Succeeded) { var msg = "User " + user.FirstName + " " + user.Surname + " has signed up in your application."; _notificationTable.NotifyUser(1, msg); var signInResult = await SignInMgr.PasswordSignInAsync(user, signUp.Password, false, false); if (signInResult.Succeeded) { HttpContext.Response.Cookies.Append( "Identity.Domain", HttpContext.Items["domain"] as string, new CookieOptions { Expires = DateTime.Now.AddDays(30), HttpOnly = true, Secure = true, SameSite = SameSiteMode.Lax } ); return(RedirectToAction("Index", "HomePage")); } } else { ModelState.AddModelError("CustomErr", $"User was created, but service of your domain is not paid. Sign in is impossible. Please contact your domain admin."); } } } else { ModelState.AddModelError("CustomErr", $"User {user.Email} already exists."); } } } else { ModelState.AddModelError("CustomErr", $"Number of users has been exceeded! If you want to register, contact domain admin to subscribe to higher service."); } } var model = new IndexViewModel { Domain = HttpContext.Items["domain"] as string, SignUpSelected = true, SignIn = new SignInForm(), SignUp = signUp }; return(View("Index", model)); }