public async Task <IActionResult> Login(LoginInputModel model)
        {
            if (ModelState.IsValid)
            {
                // validate username/password against in-memory store
                if (_loginService.ValidateCredentials(model.Username, model.Password))
                {
                    // issue authentication cookie with subject ID and username
                    var user = _loginService.FindByUsername(model.Username);
                    await HttpContext.Authentication.SignInAsync(user.Subject, user.Username);

                    // make sure the returnUrl is still valid, and if yes - redirect back to authorize endpoint
                    if (_interaction.IsValidReturnUrl(model.ReturnUrl))
                    {
                        return(Redirect(model.ReturnUrl));
                    }

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

                ModelState.AddModelError("", "Invalid username or password.");
            }

            // something went wrong, show form with error
            var vm = new LoginViewModel(HttpContext, model);

            return(View(vm));
        }
Beispiel #2
0
        public async Task <IActionResult> Login([FromForm] LoginViewModel viewModel)
        {
            if (!_loginService.ValidateCredentials(viewModel.Username, viewModel.Password))
            {
                ModelState.AddModelError("", "Invalid username or password");
                viewModel.Password = string.Empty;
                return(View("/AuthServer/Views/Login.cshtml", viewModel));
            }

            // Use an IdentityServer-compatible ClaimsPrincipal
            var principal = IdentityServerPrincipal.Create(viewModel.Username, viewModel.Username);
            await HttpContext.Authentication.SignInAsync("Cookies", principal);

            return(Redirect(viewModel.ReturnUrl));
        }
        public async Task <IActionResult> Login(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                // validate username/password against in-memory store
                if (_loginService.ValidateCredentials(model.Username, model.Password))
                {
                    // issue authentication cookie with subject ID and username
                    var user = _loginService.FindByUsername(model.Username);

                    AuthenticationProperties props = null;
                    // only set explicit expiration here if persistent.
                    // otherwise we reply upon expiration configured in cookie middleware.
                    if (model.RememberLogin)
                    {
                        props = new AuthenticationProperties
                        {
                            IsPersistent = true,
                            ExpiresUtc   = DateTimeOffset.UtcNow.AddMonths(1)
                        };
                    }
                    ;

                    await HttpContext.Authentication.SignInAsync(user.Subject, user.Username, props);

                    // make sure the returnUrl is still valid, and if yes - redirect back to authorize endpoint
                    if (_interaction.IsValidReturnUrl(model.ReturnUrl))
                    {
                        return(Redirect(model.ReturnUrl));
                    }

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

                ModelState.AddModelError("", "Invalid username or password.");
            }

            // something went wrong, show form with error
            var vm = new LoginViewModel
            {
                Tenant    = model.Tenant,
                Username  = model.Username,
                ReturnUrl = model.ReturnUrl
            };

            return(View(vm));
        }