コード例 #1
0
        public async Task <IActionResult> Register(NewUserModel model)
        {
            if (ModelState.IsValid)
            {
                EmoteLogUser user = new EmoteLogUser
                {
                    UserName       = model.UserName,
                    FirstName      = model.FirstName,
                    LastName       = model.LastName,
                    Email          = model.Email,
                    CreatedAccount = DateTime.Now
                };
                IdentityResult result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Login"));
                }
                else
                {
                    foreach (IdentityError error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }
                }
            }
            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> AdminEditRole(RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    EmoteLogUser user = await _userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await _userManager.AddToRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                }
                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    EmoteLogUser user = await _userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await _userManager.RemoveFromRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                }
            }
            if (ModelState.IsValid)
            {
                return(RedirectToAction(nameof(AdminRoleManagement)));
            }
            else
            {
                return(await AdminEditRole(model.RoleId));
            }
        }
コード例 #3
0
        public async Task <IActionResult> Login(LoginModel details, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                EmoteLogUser user = await _userManager.FindByEmailAsync(details.Email);

                if (user != null)
                {
                    Microsoft.AspNetCore.Identity.SignInResult result =
                        await _signManager.PasswordSignInAsync(user, details.Password, false, false);

                    if (result.Succeeded)
                    {
                        return(Redirect(returnUrl ?? "/"));
                    }
                }
                ModelState.AddModelError(nameof(LoginModel.Email), "Invalid user or password");
            }
            return(View(details));
        }
コード例 #4
0
        public async Task <IActionResult> AdminDeleteUser(string id)
        {
            EmoteLogUser user = await _userManager.FindByIdAsync(id);

            if (user != null)
            {
                IdentityResult result = await _userManager.DeleteAsync(user);

                if (result.Succeeded)
                {
                    return(RedirectToAction("AdminUserManagement"));
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
            else
            {
                ModelState.AddModelError("", "User Not Found");
            }
            return(View("AdminUserManagement", _userManager.Users));
        }