Example #1
0
        public async Task <IActionResult> Register(RegisterViewModel model, CancellationToken cancellationToken, string returnUrl = null)
        {
            if (Config.SystemBehaviorConfig.RedirectHomeToLogin)
            {
                return(RedirectToAction("LogOn", "Account"));
            }


            ViewBag.DepartmentTypes = new SelectList(model.DepartmentTypes);
            model.SiteKey           = WebConfig.RecaptchaPublicKey;
            ViewData["ReturnUrl"]   = returnUrl;

            if (ModelState.IsValid)
            {
                var user = new IdentityUser {
                    UserName = model.Username, Email = model.Email, SecurityStamp = Guid.NewGuid().ToString()
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    UserProfile up = new UserProfile();
                    up.UserId    = user.Id;
                    up.FirstName = model.FirstName;
                    up.LastName  = model.LastName;
                    await _userProfileService.SaveProfileAsync(0, up, cancellationToken);

                    _usersService.AddUserToUserRole(user.Id);
                    _usersService.InitUserExtInfo(user.Id);

                    Department department = await _departmentsService.CreateDepartmentAsync(model.DepartmentName, user.Id, model.DepartmentType, null, cancellationToken);

                    await _departmentsService.AddUserToDepartmentAsync(department.DepartmentId, user.Id, true, cancellationToken);

                    await _subscriptionsService.CreateFreePlanPaymentAsync(department.DepartmentId, user.Id, cancellationToken);

                    // Guard, in case testing has caching turned on for the shared redis cache there can be artifacts
                    _departmentsService.InvalidateAllDepartmentsCache(department.DepartmentId);
                    _departmentsService.InvalidateDepartmentMembers();

                    _emailMarketingProvider.SubscribeUserToAdminList(model.FirstName, model.LastName, model.Email);
                    _emailService.SendWelcomeEmail(department.Name, $"{model.FirstName} {model.LastName}", model.Email, model.Username, model.Password, department.DepartmentId);

                    var loginResult = await _signInManager.PasswordSignInAsync(model.Username, model.Password, true, lockoutOnFailure : false);

                    if (loginResult.Succeeded)
                    {
                        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, HttpContext.User, new AuthenticationProperties
                        {
                            ExpiresUtc   = DateTime.UtcNow.AddHours(24),
                            IsPersistent = false,
                            AllowRefresh = false
                        });

                        if (!String.IsNullOrWhiteSpace(returnUrl))
                        {
                            return(RedirectToLocal(returnUrl));
                        }
                        else
                        {
                            return(RedirectToAction("Dashboard", "Home", new { Area = "User" }));
                        }
                    }
                    else
                    {
                        return(View(model));
                    }
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }