Ejemplo n.º 1
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var userExists = await _userManager.FindByEmailAsync(model.Email);

            if (userExists != null)
            {
                ModelState.AddModelError(string.Empty, "User with this email was already registered.");
                return(View(model));
            }

            var newUser = new IdentityUser
            {
                Email    = model.Email,
                UserName = Guid.NewGuid().ToString()
            };
            var result = await _userManager.CreateAsync(newUser, model.Password);

            if (result.Succeeded)
            {
                await _userManager.AddToRoleAsync(newUser, SystemRoles.User.ToString());

                var newUserProfile = new SystemUser
                {
                    EntityId       = newUser.Id,
                    Name           = model.Name,
                    SelectedFolder = await _foldersProvider.GetActiveFolder()
                };
                try
                {
                    await _usersProvider.AddNewUser(newUserProfile);

                    return(RedirectToAction("Index"));
                }
                catch (Exception e)
                {
                    ModelState.AddModelError(string.Empty, e.Message);
                    return(View(model));
                }
            }

            foreach (var identityError in result.Errors)
            {
                ModelState.AddModelError(string.Empty, identityError.Description);
            }

            return(View(model));
        }