public async Task <IActionResult> Login(LoginAdminUser loginAdminUser)
        {
            if (ModelState.IsValid)
            {
                AdminUser adminUser = await userManager.FindByNameAsync(loginAdminUser.UserName);

                if (adminUser != null)
                {
                    await signInManager.SignOutAsync();

                    Microsoft.AspNetCore.Identity.SignInResult signInResult = await signInManager.PasswordSignInAsync(adminUser, loginAdminUser.Password, false, false);

                    if (signInResult.Succeeded)
                    {
                        return(RedirectToAction("Index", "Dashboard"));
                    }
                }
                ModelState.AddModelError(nameof(LoginAdminUser.UserName), "Invalid username or password");
                return(View(loginAdminUser));
            }
            else
            {
                return(View());
            }
        }
Beispiel #2
0
        public async Task <IActionResult> AddAdminUser(LoginAdminUser user)
        {
            ViewBag.Title          = "Welcome admin";
            ViewBag.DashboardCateg = "Admin Users Table";
            if (ModelState.IsValid)
            {
                IdentityResult result = await userManager.CreateAsync(new AdminUser(user.UserName), user.Password);

                if (result.Succeeded)
                {
                    if (await roleManager.RoleExistsAsync("Admins"))
                    {
                        IdentityResult result1 = await userManager.AddToRoleAsync(await userManager.FindByNameAsync(user.UserName), "Admins");

                        if (result1.Succeeded)
                        {
                            return(RedirectToAction(nameof(AdminUsers)));
                        }
                        else
                        {
                            AddErrorsFromResult(result1);
                            return(View("AdminUsers", userManager.Users));
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Role", "The role doesn't exists.");
                        return(View("AdminUsers", userManager.Users));
                    }
                }
                else
                {
                    AddErrorsFromResult(result);
                    return(View("AdminUsers", userManager.Users));
                }
            }
            else
            {
                return(View("AdminUsers", userManager.Users));//need to implement viewbag to transfer already validated fields
            }
        }