protected ContentResult RegisterLogic(UserRegistration userRegistrationParams)
        {
            Response     result;
            ResponseType responseType;
            // ensure users can request lost password
            var registrationSettings = OrchardServices.WorkContext.CurrentSite.As <RegistrationSettingsPart>();

            if (!registrationSettings.UsersCanRegister)
            {
                result = UtilsServices.GetResponse(ResponseType.None, T("Users cannot register due to site settings.").Text);
                return(UtilsServices.ConvertToJsonResult(result));
            }
            try {
                _usersExtensionsServices.Register(userRegistrationParams);
                List <string> roles   = new List <string>();
                var           message = "";
                var           registeredServicesData = UtilsServices.GetUserIdentityProviders(_identityProviders);
                var           json = registeredServicesData.ToString();
                responseType = ResponseType.Success;
                if (OrchardServices.WorkContext.CurrentUser == null && registrationSettings.UsersMustValidateEmail)
                {
                    message      = T("Thank you for registering. We sent you an e-mail with instructions to enable your account.").ToString();
                    responseType = ResponseType.ToConfirmEmail;
                }

                result = UtilsServices.GetResponse(responseType, message, json);
            }
            catch (Exception ex) {
                result = UtilsServices.GetResponse(ResponseType.None, ex.Message);
            }
            return(UtilsServices.ConvertToJsonResult(result));
        }
        protected JsonResult ChallengeEmailApiLogic(string nonce)
        {
            var      user = _userService.ValidateChallenge(nonce);
            Response result;

            if (user != null)
            {
                _userEventHandler.ConfirmedEmail(user);

                result = UtilsServices.GetResponse(ResponseType.Success, T("Email confirmed").Text);

                return(Json(result));
            }
            result = UtilsServices.GetResponse(ResponseType.None, T("Email not confirmed").Text);
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        protected ContentResult SignInLogic(UserLogin login)
        {
            Response result;

            try {
                _usersExtensionsServices.SignIn(login);
                List <string> roles = new List <string>();
                var           registeredServicesData = UtilsServices.GetUserIdentityProviders(_identityProviders);
                var           json = registeredServicesData.ToString();
                result = UtilsServices.GetResponse(ResponseType.Success, "", json);
            }
            catch (Exception ex) {
                result = UtilsServices.GetResponse(ResponseType.InvalidUser, ex.Message);
            }
            return(UtilsServices.ConvertToJsonResult(result));
        }
        protected JsonResult SignOutLogic()
        {
            Response result;

            if (OrchardServices.WorkContext.CurrentUser == null || // if the User is null the SignOutLogic do nothing and returns Success because the user is effectively not logged in
                CsrfTokenHelper.DoesCsrfTokenMatchAuthToken())
            {
                try {
                    _usersExtensionsServices.SignOut();
                    result = UtilsServices.GetResponse(ResponseType.Success);
                }
                catch (Exception ex) {
                    result = UtilsServices.GetResponse(ResponseType.InvalidUser, ex.Message);
                }
            }
            else
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(UtilsServices.GetResponse(ResponseType.InvalidXSRF)));
            }
            return(Json(result));
        }
        protected JsonResult RequestLostPasswordLogic(string username, LostPasswordUserOptions userOptions, string internationalPrefix = null)
        {
            // ensure users can request lost password
            Response result = UtilsServices.GetResponse(ResponseType.None, T("Send email failed.").Text);
            var      registrationSettings = OrchardServices.WorkContext.CurrentSite.As <RegistrationSettingsPart>();

            if (!registrationSettings.EnableLostPassword)
            {
                result = UtilsServices.GetResponse(ResponseType.None, T("Users cannot recover lost password due to site settings.").Text);

                return(Json(result));
            }

            if (String.IsNullOrWhiteSpace(username))
            {
                result = UtilsServices.GetResponse(ResponseType.None, T("Invalid user.").Text);
                return(Json(result));
            }

            var siteUrl = OrchardServices.WorkContext.CurrentSite.BaseUrl;

            if (String.IsNullOrWhiteSpace(siteUrl))
            {
                siteUrl = HttpContext.Request.ToRootUrlString();
            }

            // test if user is user/email or phone number
            if (userOptions == LostPasswordUserOptions.Account)
            {
                if (_userService.SendLostPasswordEmail(username, nonce => Url.MakeAbsolute(Url.Action("LostPassword", "Account", new { Area = "Orchard.Users", nonce = nonce }), siteUrl)))
                {
                    result = UtilsServices.GetResponse(ResponseType.Success);
                }
                else
                {
                    result = UtilsServices.GetResponse(ResponseType.None, T("Send email failed.").Text);
                }
            }
            else
            {
                var sendSmsResult = _usersExtensionsServices.SendLostPasswordSms(internationalPrefix, username, nonce => Url.MakeAbsolute(Url.Action("LostPassword", "Account", new { Area = "Orchard.Users", nonce = nonce }), siteUrl));

                if (sendSmsResult == "TRUE")
                {
                    result = UtilsServices.GetResponse(ResponseType.Success);
                }
                else
                {
                    Dictionary <string, string> errors = new Dictionary <string, string>();
                    errors.Add("BODYEXCEEDED", T("Message rejected: too many characters. (160 max)").ToString()); //"messaggio rigettato per superamento lunghezza max di testo (160 caratteri)");
                    errors.Add("MISSINGPARAMETER_1", T("Missing recipient").ToString());                          //"Destinatario mancante");
                    errors.Add("MISSINGPARAMETER_2", T("Sender identifier missing").ToString());                  //"Identificativo di invio mancante");
                    errors.Add("MISSINGPARAMETER_3", T("Sender missing or wrong").ToString());                    //"Mittente mancante o errato");
                    errors.Add("MISSINGPARAMETER_4", T("Missing text").ToString());                               //"Testo mancante");
                    errors.Add("MISSINGPARAMETER_5", T("Priority missing or wrong").ToString());                  //"Priorità mancante o errata");
                    errors.Add("FALSE", T("Generic error").ToString());                                           //"Errore generico");
                    result = UtilsServices.GetResponse(ResponseType.None, T("Send SMS failed.").Text + errors[sendSmsResult].ToString());
                }
            }
            return(Json(result));
        }