Esempio n. 1
0
        public async Task <IActionResult> Login(LoginInputVM model)
        {
            if (ModelState.IsValid)
            {
                // check password
                bool correctPassword = await _userDataService.CheckUserPassword(model.Username, model.Password);

                if (!correctPassword)
                {
                    ModelState.AddModelError("", AuthenticationOptions.InvalidCredentialsErrorMessage);
                    return(await Login(Request.Query["returnUrl"]));
                }

                // get user object
                var user = await _userDataService.GetUserByUsername(model.Username);

                await LogUserIn(user, model.RememberLogin);

                Microsoft.Extensions.Primitives.StringValues returnUrl = "";
                if (Request.Query.TryGetValue("returnUrl", out returnUrl))
                {
                    return(Redirect(Request.Query["returnUrl"]));
                }

                return(RedirectToAction("index", "home"));
            }
            ModelState.AddModelError("", AuthenticationOptions.InvalidCredentialsErrorMessage);
            return(await Login(Request.Query["returnUrl"]));
        }
Esempio n. 2
0
        public async Task <IActionResult> Login(LoginInputVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _dbContext.Users.FirstOrDefaultAsync(x => x.Username == model.Username &&
                                                                  x.Password == model.Password);

            if (user == null)
            {
                ModelState.AddModelError(nameof(model.Password), "Username or password is incorrect");
                return(View(model));
            }

            await HttpContext.SetLoggedInUser(user, model.RememberMe);

            return(RedirectToAction("Index", "MaturskiIspit"));
        }
        public async Task <IActionResult> Login(LoginInputVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _context.Users.FirstOrDefaultAsync(x =>
                                                                x.Username == model.Username && x.Password == model.Password);

            if (user == null)
            {
                ModelState.AddModelError("Password", "Pogresan username ili password.");
                return(View(model));
            }

            await HttpContext.SetLoggedInUser(user, model.RememberMe);

            return(RedirectToAction("All", "OdrzaniCas"));
        }
        public static async Task <LoginVM> BuildLoginVMAsync(ISecurableService securableService, IAuthenticationSchemeProvider schemeProvider, LoginInputVM model)
        {
            var vm = await BuildLoginVMAsync(securableService, schemeProvider, model.ReturnUrl);

            vm.Username      = model.Username;
            vm.RememberLogin = model.RememberLogin;
            return(vm);
        }
        public async Task <IActionResult> Login(LoginInputVM model, string button)
        {
            if (button != "login")
            {
                var returnUrl = await _accountService.CancelLoginAsync(model.ReturnUrl);

                return(Redirect(returnUrl));
            }

            if (ModelState.IsValid)
            {
                var result = await _accountService.LoginAsync(AutoMapper.Mapper.Map <LoginSM>(model));

                if (result.Succeeded)
                {
                    // only set explicit expiration here if user chooses "remember me".
                    // otherwise we rely upon expiration configured in cookie middleware.
                    AuthenticationProperties props = null;
                    if (AccountOptionsOM.AllowRememberLogin && model.RememberLogin)
                    {
                        props = new AuthenticationProperties
                        {
                            IsPersistent = true,
                            ExpiresUtc   = DateTimeOffset.UtcNow.Add(AccountOptionsOM.RememberMeLoginDuration)
                        };
                    }
                    ;

                    // issue authentication cookie with subject ID and username -- and roles


                    var roles = await _accountService.GetRolesForUserAsync(result.User);

                    var claimsId = new ClaimsIdentity();

                    claimsId.AddClaim(new Claim(JwtClaimTypes.Subject, result.User.Id));
                    claimsId.AddClaim(new Claim(JwtClaimTypes.Name, result.User.UserName));
                    var roleClaims = roles.Select(x => new Claim(JwtClaimTypes.Role, x));
                    claimsId.AddClaims(roleClaims);

                    await HttpContext.SignInAsync(
                        //result.User.Id,
                        new ClaimsPrincipal(claimsId),
                        props); //result.User.UserName, props);

                    // make sure the returnUrl is still valid, and if so redirect back to authorize endpoint or a local page
                    // the IsLocalUrl check is only necessary if you want to support additional local pages, otherwise IsValidReturnUrl is more strict
                    if (_accountService.IsValidReturnUrl(model.ReturnUrl) || Url.IsLocalUrl(model.ReturnUrl))
                    {
                        return(Redirect(model.ReturnUrl));
                    }

                    return(Redirect("~/"));
                }

                ModelState.AddModelError("", AccountOptionsOM.InvalidCredentialsErrorMessage);
            }
            // something went wrong, show form with error
            var vm = await LoginVMFactory.BuildLoginVMAsync(_securableService, _schemeProvider, model);

            return(View(vm));
        }