Esempio n. 1
0
        public async Task <IActionResult> Login(LoginViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure : false);

                if (result.Succeeded)
                {
                    _logger.LogInformation(1, "User logged in.");
                    return(RedirectToLocal(returnUrl));
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning(2, "User account locked out.");
                    return(View("Lockout"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Coś się nie zgadza. Upewnij się, że konto jest aktywne.");
                    return(View(model));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 2
0
        public async Task <ActionResult> Login(LoginViewModel loginView, string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            if (!ModelState.IsValid)
            {
#if DEBUG
                Debug.WriteLine("Model is NOT valid!");
#endif
                return(View());
            }
#if DEBUG
            Debug.WriteLine("Mode is valid");
#endif

            //_userManager.

            //var result =await SignInManager.PasswordSignInAsync(loginView.UserName, loginView.Password, false,shouldLockout: false);
            var result =
                await
                _signInManager.PasswordSignInAsync(loginView.UserName, loginView.Password, false,
                                                   shouldLockout : true);

#if DEBUG
            Debug.WriteLine("PasswordSignInAsync result=" + result.ToString());
            Debug.WriteLine("PasswordSignInAsync returnUrl=" + returnUrl);
#endif


            switch (result)
            {
            case SignInCode.Success:
#if DEBUG
                Debug.WriteLine("++ ready to RedirectToLocal");
#endif
                return(RedirectToLocal(returnUrl));

            case SignInCode.LockedOut:
#if DEBUG
                Debug.WriteLine("Redirect to Lockout");
#endif
                return(View("Lockout"));

            case SignInCode.ChangePassword:
                return(View("ChangePassword"));

            default:
                ModelState.AddModelError("", "Пароль или логин неверны");
//                    ViewBag.ReturnUrl = returnUrl;
                return(View(loginView));
            }

            /*var user = await _userManager.FindByNameAsync(loginView.UserName);
             * if (user != null)
             * {
             *  Debug.WriteLine("user is NOT null");
             *
             *  if (await _userManager.CheckPasswordAsync(user, loginView.Password))
             *  {
             *      Debug.WriteLine("redirect!!!");
             *      IAuthenticationManager authenticationManager = HttpContext.GetOwinContext().Authentication;
             *      ClaimsIdentity identity = _userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
             *      authenticationManager.SignIn(identity);
             *      return RedirectToAction("Index", "Home");
             *  }
             *
             * }
             #if DEBUG
             * else
             * {
             *  Debug.WriteLine("user is NULL!");
             * }
             #endif*/
            //return View();
        }
Esempio n. 3
0
        /// <summary>
        /// Realiza el inicio de sesión para un usuario con una password.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="password"></param>
        /// <param name="isPersistent">Habilitar o no cookie para sesión persistente, tras cerrar navegador</param>
        /// <returns></returns>
        public async Task <SignInResult> LogInCookieAsync(MyUser user, string password, bool isPersistent = true)
        {
            var result = await sM.PasswordSignInAsync(user, password, isPersistent, false);

            return(result);
        }