Example #1
0
        public ActionResult Login(LoginViewModel model, string returnUrl = null)
        {
            if (User?.Identity?.IsAuthenticated == true)
            {
                this.ShowMessage(MessageType.Warning, Resource.AlreadyLogIn);

                returnUrl = GetDefaultUrl();
                if (Request.IsAjaxRequest())
                {
                    this.SetAjaxResponseRedirectUrl(returnUrl, true);
                    return(new EmptyResult());
                }

                return(Redirect(returnUrl));
            }

            // Validate captcha control
            if (!captchaService.Validate(Request["g-recaptcha-response"]))
            {
                ModelState.AddModelErrorSafety(string.Empty, Resource.WrongCaptchaMessage);
            }
            else
            {
                var errors = authenticationProvider.Login(model.Username, model.Password);
                if (errors != null)
                {
                    ModelState.Merge(errors);
                }
            }

            if (!ModelState.IsValid)
            {
                return(Request.IsAjaxRequest()
                    ? PartialView("Login", model) as ActionResult
                    : View("Login", model));
            }

            returnUrl = returnUrl.IsNotNullOrEmpty() && Url.IsLocalUrl(returnUrl)
                ? returnUrl
                : GetDefaultUrl();

            if (Request.IsAjaxRequest())
            {
                this.SetAjaxResponseRedirectUrl(returnUrl, true);
                return(new EmptyResult());
            }

            return(Redirect(returnUrl));
        }