示例#1
0
        public async Task <DepartmentCreationResult> RegisterDepartment([FromBody] DepartmentCreationInput model)
        {
            DepartmentCreationResult creationResult = new DepartmentCreationResult();
            var user = new Microsoft.AspNet.Identity.EntityFramework6.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;

                var names = model.FullName.Split(char.Parse(" "));

                if (names.Length > 1)
                {
                    up.FirstName = names[0];
                    up.LastName  = names[1];
                }
                else
                {
                    up.FirstName = model.FullName;
                    up.LastName  = "";
                }
                _userProfileService.SaveProfile(0, up);

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

                var savedUser = await _userManager.FindByIdAsync(user.Id);

                Department department = _departmentsService.CreateDepartment(model.DepartmentName, user.Id, model.DepartmentType);

                //_departmentsService.AddUserToDepartment(model.DepartmentName, user.Id);
                _departmentsService.AddUserToDepartment(department.DepartmentId, user.Id);
                _subscriptionsService.CreateFreePlanPayment(department.DepartmentId, user.Id);
                _emailMarketingProvider.SubscribeUserToAdminList(up.FirstName, up.LastName, model.Email);
                _departmentsService.InvalidateDepartmentMembers();

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


                creationResult.Successful = true;
            }
            else
            {
                creationResult.Successful = false;
            }

            return(creationResult);
        }
示例#2
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            if (Config.SystemBehaviorConfig.RedirectHomeToLogin)
            {
                return(RedirectToAction("LogOn", "Account"));
            }

            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new Microsoft.AspNet.Identity.EntityFramework6.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;
                    _userProfileService.SaveProfile(0, up);

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

                    var savedUser = await _userManager.FindByIdAsync(user.Id);

                    Department department = _departmentsService.CreateDepartment(model.DepartmentName, user.Id, model.DepartmentType);
                    _departmentsService.AddUserToDepartment(model.DepartmentName, user.Id);
                    _subscriptionsService.CreateFreePlanPayment(department.DepartmentId, user.Id);
                    _emailMarketingProvider.SubscribeUserToAdminList(model.FirstName, model.LastName, model.Email);

                    _departmentsService.InvalidateDepartmentMembers();

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

                    //await _signInManager.SignInAsync(savedUser, isPersistent: false);
                    //return RedirectToLocal(returnUrl);

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

                    if (result.Succeeded)
                    {
                        await HttpContext.Authentication.SignInAsync("ResgridCookieMiddlewareInstance", 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));
        }