public async Task <IActionResult> Login(LoginViewModel model, string returnURL)
        {
            if (ModelState.IsValid)
            {
                var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, isPersistent : model.RememberMe, false);

                if (result.Succeeded)
                {
                    if (!string.IsNullOrWhiteSpace(returnURL) && Url.IsLocalUrl(returnURL))
                    {
                        return(Redirect(returnURL));
                    }
                    return(RedirectToAction(nameof(HomeController.Index), ControllerNameOutput.ToString(nameof(HomeController))));
                }

                ModelState.AddModelError(string.Empty, "Invalid login attempt");
            }
            return(View(model));
        }
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            var exists = await _userManager.FindByEmailAsync(model.Email);

            if (exists != null)
            {
                return(RedirectToAction(nameof(Register),
                                        ControllerNameOutput.ToString(nameof(AccountController))));
            }

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.UserName,
                    Email    = model.Email,
                    City     = model.City
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    if (_signInManager.IsSignedIn(User) && User.IsInRole(RoleConstants.Admin))
                    {
                        return(RedirectToAction(nameof(AdministrationController.ListUsers),
                                                ControllerNameOutput.ToString(nameof(AdministrationController))));
                    }

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(RedirectToAction(nameof(HomeController.Index), ControllerNameOutput.ToString(nameof(HomeController))));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }
            return(View(model));
        }
Esempio n. 3
0
        public async Task <IActionResult> CreateRole(CreateRoleViewModel model)
        {
            if (ModelState.IsValid)
            {
                var identityRole = new IdentityRole()
                {
                    Name = model.RoleName,
                };
                var result = await _roleManager.CreateAsync(identityRole);

                if (result.Succeeded)
                {
                    return(RedirectToAction(nameof(ListRoles), ControllerNameOutput.ToString(nameof(AdministrationController))));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }

            return(View(model));
        }
        public async Task <IActionResult> Logout()
        {
            await _signInManager.SignOutAsync();

            return(RedirectToAction(nameof(HomeController.Index), ControllerNameOutput.ToString(nameof(HomeController))));
        }