Ejemplo n.º 1
0
        public async Task <IActionResult> Create(CreateUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                AppIdentityUser newUser = new AppIdentityUser()
                {
                    Email       = model.Email,
                    ClassId     = model.ClassId,
                    UserName    = model.Email,
                    FirstName   = model.FirstName,
                    MiddleName  = model.MiddleName,
                    LastName    = model.LastName,
                    PhoneNumber = model.PhoneNumber
                };
                var result = await _userManager.CreateAsync(user : newUser, password : model.Password);

                if (result.Succeeded)
                {
                    string code = await _userManager.GenerateEmailConfirmationTokenAsync(newUser);

                    string linkConfirm = Url.Action(action: "VerifyEmail",
                                                    controller: "Account",
                                                    new { userId = newUser.Id, code = code },
                                                    Request.Scheme,
                                                    Request.Host.ToString());
                    EmailUtilities.SendConfirmEmail(newUser, linkConfirm);
                    if (model.RoleIds.Any() && model.RoleIds != null)
                    {
                        List <string> rolesName = new List <string>();
                        foreach (string roleId in model.RoleIds)
                        {
                            var role = await _roleManager.FindByIdAsync(roleId);

                            rolesName.Add(role.Name);
                        }
                        var addRoleResult = await _userManager.AddToRolesAsync(newUser, rolesName);

                        if (addRoleResult.Succeeded)
                        {
                            return(RedirectToAction(actionName: "Manage"));
                        }
                        foreach (var error in addRoleResult.Errors)
                        {
                            ModelState.AddModelError("", error.Description);
                        }
                        return(View(model));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }
            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Register(RegisterAccountViewModel model)
        {
            if (ModelState.IsValid)
            {
                AppIdentityUser newUser = new AppIdentityUser()
                {
                    UserName    = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    MiddleName  = model.MiddleName,
                    Email       = model.Email,
                    PhoneNumber = model.PhoneNumber,
                    ClassId     = model.ClassId
                };
                var result = await _userManager.CreateAsync(newUser, model.Password);

                if (result.Succeeded)
                {
                    await _userManager.AddToRoleAsync(user : newUser, role : "Normal User");

                    string code = await _userManager.GenerateEmailConfirmationTokenAsync(newUser);

                    string linkConfirm = Url.Action(action: "VerifyEmail",
                                                    controller: "Account",
                                                    new { userId = newUser.Id, code = code },
                                                    Request.Scheme,
                                                    Request.Host.ToString());
                    EmailUtilities.SendConfirmEmail(newUser, linkConfirm);
                    return(RedirectToAction(actionName: "SentConfirmEmail"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }
            model.Classes = _context.Classes.Select(e => e).ToList();
            return(View(model));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Edit(EditUserViewModel model)
        {
            AppIdentityUser user = await _userManager.FindByIdAsync(model.UserId);

            if (user == null)
            {
                return(NotFound());
            }
            user.FirstName   = model.FirstName;
            user.MiddleName  = model.MiddleName;
            user.LastName    = model.LastName;
            user.ClassId     = model.ClassId;
            user.PhoneNumber = model.PhoneNumber;
            //when change email must confirm link in old email
            if (user.Email != model.Email)
            {
                string tokenChangeEmail = await _userManager.GenerateChangeEmailTokenAsync(user, model.Email);

                string linkConfirm = Url.Action(action: "VerifyEmailChange",
                                                controller: "Account",
                                                new { userId = user.Id, newEmail = model.Email, code = tokenChangeEmail },
                                                Request.Scheme,
                                                Request.Host.ToString());
                EmailUtilities.SendConfirmEmail(user, linkConfirm);
            }
            if (model.RoleIds.Any() && model.RoleIds != null)
            {
                List <string> rolesName = new List <string>();
                foreach (string roleId in model.RoleIds)
                {
                    var role = await _roleManager.FindByIdAsync(roleId);

                    rolesName.Add(role.Name);
                }
                IEnumerable <string> userRolesName = await _userManager.GetRolesAsync(user);

                if (userRolesName.Any() && userRolesName != null)
                {
                    var removeRoleResult = await _userManager.RemoveFromRolesAsync(user, userRolesName);

                    if (!removeRoleResult.Succeeded)
                    {
                        foreach (var error in removeRoleResult.Errors)
                        {
                            ModelState.AddModelError("", error.Description);
                        }
                    }
                }
                var addRoleResult = await _userManager.AddToRolesAsync(user, rolesName);

                if (addRoleResult.Succeeded)
                {
                    return(RedirectToAction(actionName: "Manage"));
                }
                foreach (var error in addRoleResult.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
                return(View(model));
            }
            var resultEditUser = await _userManager.UpdateAsync(user);

            if (resultEditUser.Succeeded)
            {
                return(RedirectToAction(actionName: "Manage"));
            }
            foreach (var error in resultEditUser.Errors)
            {
                ModelState.AddModelError("", error.Description);
            }
            return(View(model));
        }