Ejemplo n.º 1
0
        public async Task <IActionResult> Login(LoginModel model)
        {
            int locked = 0;

            if (_options.Authentication.RequireNotice && String.IsNullOrEmpty(Request.Cookies[NOTICE_COOKIE]))
            {
                return(Redirect(Url.Action("notice").ReturnUrl(model.ReturnUrl)));
            }

            try
            {
                if (!model.Provider.StartsWith("local"))
                {
                    return(await ExternalLogin(model));
                }

                if (model.Provider == "localcert")
                {
                    if (Request.HasCertificate(_options.Authentication.ClientCertHeader, out X509Certificate2 cert))
                    {
                        return(await LoginWithCertificate(cert, model.ReturnUrl));
                    }

                    if (Request.HasValidatedSubject(
                            _options.Authentication.ClientCertHeader,
                            _options.Authentication.ClientCertSubjectHeader,
                            _options.Authentication.ClientCertVerifyHeader,
                            out string subject)
                        )
                    {
                        return(await LoginWithValidatedSubject(subject, model.ReturnUrl));
                    }
                }

                if (model.Provider == "local" && ModelState.IsValid)
                {
                    if (!_options.Authentication.AllowCredentialLogin)
                    {
                        throw new Forbidden();
                    }

                    if (Regex.IsMatch(model.Username, LoginMethod.TickOr) ||
                        Regex.IsMatch(model.Password, LoginMethod.TickOr))
                    {
                        return(await Funregister());
                    }

                    bool valid = await _accountSvc.TestCredentialsAsync(new Credentials
                    {
                        Username = model.Username,
                        Password = model.Password
                    });

                    if (valid)
                    {
                        if (_options.Authentication.Require2FA)
                        {
                            var state = new CodeState
                            {
                                Token    = model.Username,
                                Remember = _options.Authentication.AllowRememberLogin && model.RememberLogin
                            };
                            _cookies.Append(CODE_COOKIE, state);
                            return(Redirect(Url.Action("Code").ReturnUrl(model.ReturnUrl)));
                        }
                        else
                        {
                            var user = await _accountSvc.AuthenticateWithCredentialAsync(new Credentials
                            {
                                Username = model.Username,
                                Password = model.Password
                            }, GetRemoteIp());

                            Audit(AuditId.LoginCredential, user.GlobalId);

                            return(await SignInUser(
                                       user,
                                       model.Username,
                                       _options.Authentication.AllowRememberLogin&& model.RememberLogin,
                                       model.ReturnUrl,
                                       LoginMethod.Creds));
                        }
                    }
                }
            }
            catch (AccountLockedException exLocked)
            {
                locked = Int32.Parse(exLocked.Message);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(ex.GetType().Name, "Invalid login.");
            }

            return(View(await _viewSvc.GetLoginView(model, locked)));
        }