예제 #1
0
        public async Task <IActionResult> Index(InputModelLogin model)
        {
            _model = model;
            if (ModelState.IsValid)
            {
                var result = await _user.UserLoginAsync(model);

                if (result.Succeeded)
                {
                    return(Redirect("/Principal/Principal"));
                }
                else
                {
                    _model.ErrorMessage = "Correo o contraseña inválidos.";
                    return(Redirect("/"));
                }
            }
            else
            {
                foreach (var modelState in ModelState.Values)
                {
                    foreach (var error in modelState.Errors)
                    {
                        _model.ErrorMessage = error.ErrorMessage;
                    }
                }
                return(Redirect("/"));
            }
        }
        public async Task <IActionResult> Zaloguj([Bind("Email,Password")] InputModelLogin model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.FindByEmailAsync(model.Email);

                if (user == null)
                {
                    ModelState.AddModelError(string.Empty, "Logowanie przebiegło niepomyślnie.");
                    _context.Logi.Add(stworzLog(await _context.Uzytkownicy.FirstOrDefaultAsync(m => m.email == "niezidentyfikowany"),
                                                "logowanie niepomyślne, nieodnaleziono uzytkownika"));
                    _context.SaveChanges();
                    return(View());
                }

                var result = await _signInManager.PasswordSignInAsync(user.id_uzytkownika.ToString(), model.Password, false, lockoutOnFailure : true);

                if (result.Succeeded)
                {
                    _context.Logi.Add(stworzLog(user, "logowanie pomyslne"));
                    _context.SaveChanges();
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    _context.Logi.Add(stworzLog(user, "logowanie niepomyślne, błędne hasło"));
                    _context.SaveChanges();
                    ModelState.AddModelError(string.Empty, "Logowanie przebiegło niepomyślnie");
                }
            }

            return(View());
        }
예제 #3
0
        internal async Task <SignInResult> UserLoginAsync(InputModelLogin model)
        {
            var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, lockoutOnFailure : false);

            if (result.Succeeded)
            {
            }
            return(result);
        }
예제 #4
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            InputLogin    = new InputModelLogin();
            InputRegister = new InputModelRegister();

            // Username and password from phone number
            InputLogin.Username           = InputModel.PhoneNumber;
            InputLogin.Password           = InputModel.PhoneNumber;
            InputRegister.UserName        = InputModel.PhoneNumber;
            InputRegister.Password        = InputModel.PhoneNumber;
            InputRegister.FullPhoneNumber = InputModel.PhoneNumber;

            // try login
            if (ModelState.IsValid)
            {
                var result = await _signInManager.PasswordSignInAsync(InputLogin.Username, InputLogin.Password, false, lockoutOnFailure : true);

                if (result.Succeeded)
                {
                    // try verify
                    var user = new ApplicationUser {
                        UserName = InputRegister.UserName, PhoneNumber = InputRegister.FullPhoneNumber
                    };

                    HttpContext.Session.SetString("_UserId", user.Id);
                    await _signInManager.PasswordSignInAsync(InputRegister.UserName, InputRegister.Password, false, lockoutOnFailure : true);

                    return(LocalRedirect(Url.Content($"~/Identity/Account/Verify/?phoneNumber={user.PhoneNumber}&returnUrl={returnUrl}")));


                    return(LocalRedirect(returnUrl));
                }
                if (result.IsLockedOut)
                {
                    return(RedirectToPage("./Lockout"));
                }

                // try register
                var userRegister = new ApplicationUser {
                    UserName = InputRegister.UserName, PhoneNumber = InputRegister.FullPhoneNumber
                };
                var resultRegister = await _userManager.CreateAsync(userRegister, InputRegister.Password);

                if (resultRegister.Succeeded)
                {
                    HttpContext.Session.SetString("_UserId", userRegister.Id);
                    await _signInManager.PasswordSignInAsync(InputRegister.UserName, InputRegister.Password, false, lockoutOnFailure : true);

                    return(LocalRedirect(Url.Content($"~/Identity/Account/Verify/?phoneNumber={userRegister.PhoneNumber}&returnUrl={returnUrl}")));

                    await _userManager.DeleteAsync(userRegister);
                }
                foreach (var error in resultRegister.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }