public async Task <ActionResult> Register(CustomerRegister customerRegister) { string OrganizationName = (string)RouteData.Values["OrganizationName"]; Organization orgExist = await _organizationRepository.OraganizationExist(OrganizationName); ViewBag.Organization = OrganizationName; if (orgExist != null) { var user = new ApplicationUser { Email = customerRegister.Email, DatabaseName = OrganizationName, UserName = customerRegister.UserName, OrganizationId = orgExist.Id, }; var result = await _userManager.CreateAsync(user, customerRegister.Password); if (result.Succeeded) { var roleCustomer = "Customer"; var claims = new List <Claim> { new Claim(ClaimTypes.Name, user.UserName), new Claim(ClaimTypes.NameIdentifier, user.Id), new Claim(ClaimTypes.Role, roleCustomer), new Claim("organization", orgExist.Name), }; var claimsIdentity = new ClaimsIdentity(claims, OrganizationName); var authProperties = new AuthenticationProperties(); await Task.WhenAll( new Task[] { _management.AddRoleToUserAsync(roleCustomer, user), HttpContext.SignInAsync(OrganizationName, new ClaimsPrincipal(claimsIdentity), authProperties) }); var Customer = new Customer() { customer_id = user.Id, name = customerRegister.name, phone_number = customerRegister.phoneNumber, email = customerRegister.Email, DateOfBirth = customerRegister.DateOfBirth, loyality_points = 0, type = 0, is_lead = false, }; byte[] error = new byte[500]; _customerRepository.setConnectionString(OrganizationName); int status = await _customerRepository.Create(Customer, error); if (status != 0) { return(StatusCode(500)); } muserLogger.LogInformation("A user with a specifc roles : " + roleCustomer + " has Been Created"); return(LocalRedirect("~/Store/" + orgExist.Name)); } var errors = result.Errors.ToList(); foreach (var el in errors) { ModelState.AddModelError("", el.Code); } } else { return(NotFound("This organization does not exist")); } return(View("CustomerRegister")); }
public async Task <IActionResult> Register(IndexViewModel mod) { Register registerModel = mod.Register; Organization organizationExist = await _organizationRepository.OraganizationExist(registerModel.DatabaseName); if (organizationExist == null) { var user = new ApplicationUser { Email = registerModel.Email, DatabaseName = registerModel.DatabaseName, Country = registerModel.Country, Language = registerModel.Language, UserName = registerModel.UserName, Organization = new Organization { Name = registerModel.DatabaseName, Email = registerModel.Email } }; var result = await _userManager.CreateAsync(user, registerModel.Password); if (result.Succeeded) { List <Task> tasks = new List <Task>(); var roleAdmin = "Administrator"; var roleEmployee = "Employee"; await _management.AddRoleToUserAsync(roleAdmin, user); await _management.AddRoleToUserAsync(roleEmployee, user); await _userManager.AddClaimAsync(user, new Claim("organization", user.DatabaseName)); await _userManager.AddClaimAsync(user, new Claim("organizationId", user.OrganizationId)); muserLogger.LogInformation("A user with a specifc roles : " + roleAdmin + " has Been Created"); var res = await _signInManager.PasswordSignInAsync(user.UserName, registerModel.Password, true, false); if (res.Succeeded) { tasks.Add(_databaseBuilder.createModulesDatabaseAsync(registerModel.DatabaseName)); tasks.Add(Task.Run(() => _authenticationProvider.AddScheme(new AuthenticationScheme(registerModel.DatabaseName, registerModel.DatabaseName, typeof(CookieAuthenticationHandler))))); ; await Task.WhenAll(tasks); using (var scope = _service.CreateScope()) { var _sysServices = scope.ServiceProvider.GetRequiredService <IServiceCollection>(); var shema = _sysServices.AddAuthentication() .AddCookie(registerModel.DatabaseName, o => { o.ExpireTimeSpan = TimeSpan.FromHours(1); o.LoginPath = new PathString("/store/{OrganizationName}"); o.Cookie.Name = registerModel.DatabaseName + " CustomerCookie"; o.SlidingExpiration = true; }); } return(RedirectToAction("System", "App")); } } var errors = result.Errors.ToList(); foreach (var el in errors) { ModelState.AddModelError("", el.Code); } } else { ModelState.AddModelError("", "This Database Name is used"); } return(View()); }