public ActionResult LogOn(LogOnModel model, string returnUrl, string InvisibleCaptchaValue)
        {
            if (!CaptchaController.IsInvisibleCaptchaValid(InvisibleCaptchaValue))
            {
                ModelState.AddModelError(string.Empty, "Captcha error.");
                return(View());
            }

            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    var u = _db.Users.FirstOrDefault(x => x.UserName == model.UserName);
                    LS.Authorize(u);

                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") &&
                        !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return(Redirect(returnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("DomainPage", "Page", new { name = "root" }));
                    }
                }
                else
                {
                    ModelState.AddModelError("", LocalizationHelpers.GetLocalResource("~/Views/Account/LogOn.cshtml", "UsernameIncorrect"));
                }
            }

            return(View(model));
        }
 public ActionResult LogOnAjx(LogOnModel model, string returnUrl)
 {
     if (ModelState.IsValid)
     {
         if (Membership.ValidateUser(model.UserName, model.Password))
         {
             var u = _db.Users.FirstOrDefault(x => x.UserName == model.UserName);
             LS.Authorize(u);
             bool haveOld = false;
             if (LS.CurrentHttpContext.Request.Cookies["SALcart"] != null)
             {
                 haveOld = true;
             }
             if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") &&
                 !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
             {
                 return(Json(new { result = "ok", url = returnUrl, haveOld = haveOld }));
             }
             else
             {
                 return(Json(new { result = "ok", haveOld = haveOld, url = Url.Action("DomainPage", "Page", new { name = "root" }) }));
                 //  eturn RedirectToAction("DomainPage", "Page", new { name = "root" });
             }
         }
         else
         {
             ModelState.AddModelError("", RP.T("Account.Logon.PasswordOrUserIncorrect").ToString());
         }
     }
     return(Json(new { result = "error", message = ModelState.Values.Select(x => new { Value = x.Value != null ? x.Value.AttemptedValue : "", errors = x.Errors.Select(y => y.ErrorMessage) }) }));
     //  return View(model);
 }
        public ActionResult GoogleAjx(string ID, string Name, string Image, string Email)
        {
            // google 111929910329405075066
            // long   9223372036854775807
            if (!string.IsNullOrEmpty(ID) && ID != "0")
            {
                string email = Email;
                if (string.IsNullOrEmpty(email))
                {
                    email = ID + "@gmail.com.com";
                }


                User uLogin = _db.Users.FirstOrDefault(r => r.GoogleID == ID);
                if (uLogin != null)
                {
                    //login
                    LS.Authorize(uLogin);
                    // var messService = new MessageService(_db);
                    // messService.SendUserRegisterEmailToUser(u);
                    return(Json(new { result = "ok", url = "" }));
                }
                else
                {
                    //register

                    //generate code
                    var chars    = "0123456789abcdefghjklmnopqrstuwvxyzQAZWSXEDCRFVTGBYHNUJMIKLOP@$&%";
                    var random   = new Random();
                    var password = new string(
                        Enumerable.Repeat(chars, 8)
                        .Select(s => s[random.Next(s.Length)])
                        .ToArray());

                    User uExist = _db.Users.FirstOrDefault(r => r.UserName == email || r.Email == email);
                    if (uExist != null)
                    {
                        uExist.GoogleID = ID;

                        _db.SaveChanges();//update facebook ID
                        //login
                        LS.Authorize(uExist);

                        return(Json(new { result = "ok", url = "" }));
                    }
                    MembershipCreateStatus createStatus;
                    MembershipUser         newUser = Membership.CreateUser(email, password, email, "-", "-", true, out createStatus);

                    if (createStatus != MembershipCreateStatus.Success)
                    {
                        ModelState.AddModelError(string.Empty, RP.T("Account.Register.Error." + createStatus.ToString()).ToString());
                    }
                    else
                    {
                        User u = _db.Users.FirstOrDefault(r => r.UserName == email);
                        u.Roles     = SF.RolesStringAdd(u.Roles, "Register");
                        u.FirstName = "";
                        u.LastName  = "";
                        if (!string.IsNullOrEmpty(Name))
                        {
                            string[] firstLast = Name.Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
                            if (firstLast.Length > 0)
                            {
                                u.FirstName = firstLast[0];
                            }
                            if (firstLast.Length > 1)
                            {
                                u.LastName = firstLast[1];
                            }
                        }
                        u.RoleDefault      = "Register";
                        u.GoogleID         = ID;
                        u.IsApproved       = true;
                        _db.Entry(u).State = EntityState.Modified;
                        _db.SaveChanges();

                        Newsletter n = new Newsletter()
                        {
                            NewsletterAccept = true,
                            NewsletterDate   = DateTime.UtcNow,
                            NewsletterEmail  = u.Email,
                            NewsletterName   = u.FirstName + " " + u.LastName,
                            RoleDefault      = "Register"
                        };
                        _db.Newsletters.Add(n);
                        _db.SaveChanges();
                        SF.AddToNewsletter(n);


                        if (Membership.ValidateUser(u.UserName, u.Password))
                        {
                            LS.Authorize(u);

                            var messService = new MessageService(_db);
                            messService.SendUserRegisterEmailToUser(u);
                            return(Json(new { result = "ok", url = "" }));
                        }
                        else
                        {
                            ModelState.AddModelError("", RP.T("Account.Logon.PasswordOrUserIncorrect").ToString());
                        }
                    }
                }
                return(Json(new { result = "error", message = ModelState.Values.Select(x => new { Value = x.Value != null ? x.Value.AttemptedValue : "", errors = x.Errors.Select(y => y.ErrorMessage) }) }));
            }
            return(Json(new { result = "error", message = ModelState.Values.Select(x => new { Value = x.Value != null ? x.Value.AttemptedValue : "", errors = x.Errors.Select(y => y.ErrorMessage) }) }));
        }
        public ActionResult RegAjx(RegisterAjxModel model)
        {
            if (ModelState.IsValid)
            {
                MembershipCreateStatus createStatus;
                MembershipUser         newUser = Membership.CreateUser(model.Email, model.Password, model.Email, "-", "-", true, out createStatus);

                if (createStatus != MembershipCreateStatus.Success)
                {
                    ModelState.AddModelError(string.Empty, RP.T("Account.Register.Error." + createStatus.ToString()).ToString());
                }
                else
                {
                    User u = _db.Users.FirstOrDefault(r => r.UserName == model.Email);
                    u.Roles       = SF.RolesStringAdd(u.Roles, "Register");
                    u.FirstName   = model.FirstName.Trim();
                    u.LastName    = model.LastName.Trim();
                    u.RoleDefault = "Register";
                    if (Session["address"] != null)
                    {
                        u.AddressMap = (string)Session["address"];
                    }
                    if (Session["longitude"] != null)
                    {
                        u.Longitude = (decimal)Session["longitude"];
                    }
                    if (Session["latitude"] != null)
                    {
                        u.Latitude = (decimal)Session["latitude"];
                    }

                    u.IsApproved       = true;
                    _db.Entry(u).State = EntityState.Modified;
                    _db.SaveChanges();
                    if (model.NewsLetter)
                    {
                        Newsletter n = new Newsletter()
                        {
                            NewsletterAccept = true,
                            NewsletterDate   = DateTime.UtcNow,
                            NewsletterEmail  = u.Email,
                            NewsletterName   = u.FirstName + " " + u.LastName,
                            RoleDefault      = "Register"
                        };
                        _db.Newsletters.Add(n);
                        _db.SaveChanges();
                        SF.AddToNewsletter(n);
                    }
                    if (Membership.ValidateUser(u.UserName, u.Password))
                    {
                        LS.Authorize(u);
                        var messService = new MessageService(_db);

                        messService.SendUserRegisterEmailToUser(u);
                        return(Json(new { result = "ok", url = "" }));
                    }
                    else
                    {
                        ModelState.AddModelError("", RP.T("Account.Logon.PasswordOrUserIncorrect").ToString());
                    }
                }
            }
            return(Json(new { result = "error", message = ModelState.Values.Select(x => new { Value = x.Value != null ? x.Value.AttemptedValue : "", errors = x.Errors.Select(y => y.ErrorMessage) }) }));

            //for future needed
            //   return Json(new { result = "error", message = new Dictionary<string,List<string>>().Select(x=> new { Value = x.Key,errors=x.Value  }) });
        }
        public ActionResult FacebookAjx(string token)
        {
            try
            {
                if (!string.IsNullOrEmpty(token))
                {
                    WebClient client     = new WebClient();
                    string    JsonResult = client.DownloadString(string.Concat(
                                                                     "https://graph.facebook.com/me?access_token=", token));

                    JObject jsonUserInfo = JObject.Parse(JsonResult);

                    FacebookProfile fp = new FacebookProfile();
                    fp.FacebookUsername = jsonUserInfo.Value <string>("username");
                    fp.FacebookEmail    = jsonUserInfo.Value <string>("email");
                    fp.FacebookLocale   = jsonUserInfo.Value <string>("locale");
                    fp.FacebookID       = jsonUserInfo.Value <long>("id");
                    fp.FacebookToken    = token;

                    fp.FacebookName        = jsonUserInfo.Value <string>("name");
                    fp.FacebookFirstName   = jsonUserInfo.Value <string>("first_name");
                    fp.FacebookLastName    = jsonUserInfo.Value <string>("last_name");
                    fp.FacebookLink        = jsonUserInfo.Value <string>("link");
                    fp.FacebookGender      = jsonUserInfo.Value <string>("gender");
                    fp.FacebookTimezone    = jsonUserInfo.Value <int>("timezone");
                    fp.FacebookVerified    = jsonUserInfo.Value <bool>("verified");
                    fp.FacebookUpdatedTime = jsonUserInfo.Value <DateTime>("updated_time");

                    User uLogin = _db.Users.FirstOrDefault(r => r.FacebookID == fp.FacebookID);
                    if (uLogin != null)
                    {
                        //login
                        LS.Authorize(uLogin);
                        // var messService = new MessageService(_db);
                        // messService.SendUserRegisterEmailToUser(u);
                        return(Json(new { result = "ok", url = "" }));
                    }
                    else
                    {
                        //register
                        string email = fp.FacebookEmail;
                        //generate code
                        var chars    = "0123456789abcdefghjklmnopqrstuwvxyzQAZWSXEDCRFVTGBYHNUJMIKLOP@$&%";
                        var random   = new Random();
                        var password = new string(
                            Enumerable.Repeat(chars, 8)
                            .Select(s => s[random.Next(s.Length)])
                            .ToArray());
                        if (string.IsNullOrEmpty(email))
                        {
                            if (!string.IsNullOrEmpty(fp.FacebookUsername))
                            {
                                email = fp.FacebookUsername + "@facebook.com";
                            }
                            else
                            {
                                email = fp.FacebookID.ToString() + "@facebook.com";
                            }
                        }
                        User uExist = _db.Users.FirstOrDefault(r => r.UserName == email);
                        if (uExist != null)
                        {
                            uExist.FacebookID = fp.FacebookID;
                            _db.SaveChanges();//update facebook ID
                            //login
                            LS.Authorize(uExist);

                            return(Json(new { result = "ok", url = "" }));
                        }
                        MembershipCreateStatus createStatus;
                        MembershipUser         newUser = Membership.CreateUser(email, password, email, "-", "-", true, out createStatus);

                        if (createStatus != MembershipCreateStatus.Success)
                        {
                            ModelState.AddModelError(string.Empty, RP.T("Account.Register.Error." + createStatus.ToString()).ToString());
                        }
                        else
                        {
                            User u = _db.Users.FirstOrDefault(r => r.UserName == email);
                            u.Roles            = SF.RolesStringAdd(u.Roles, "Register");
                            u.FirstName        = fp.FacebookFirstName;
                            u.LastName         = fp.FacebookLastName;
                            u.RoleDefault      = "Register";
                            u.FacebookID       = fp.FacebookID;
                            u.IsApproved       = true;
                            _db.Entry(u).State = EntityState.Modified;
                            _db.SaveChanges();

                            Newsletter n = new Newsletter()
                            {
                                NewsletterAccept = true,
                                NewsletterDate   = DateTime.UtcNow,
                                NewsletterEmail  = u.Email,
                                NewsletterName   = u.FirstName + " " + u.LastName,
                                RoleDefault      = "Register"
                            };
                            _db.Newsletters.Add(n);
                            _db.SaveChanges();
                            SF.AddToNewsletter(n);

                            if (Membership.ValidateUser(u.UserName, u.Password))
                            {
                                LS.Authorize(u);
                                var messService = new MessageService(_db);

                                messService.SendUserRegisterEmailToUser(u);
                                return(Json(new { result = "ok", url = "" }));
                            }
                            else
                            {
                                ModelState.AddModelError("", RP.T("Account.Logon.PasswordOrUserIncorrect").ToString());
                            }
                        }
                    }
                    return(Json(new { result = "error", json = JsonResult, facebook = fp, message = ModelState.Values.Select(x => new { Value = x.Value != null ? x.Value.AttemptedValue : "", errors = x.Errors.Select(y => y.ErrorMessage) }) }));
                }
                return(Json(new { result = "error", message = ModelState.Values.Select(x => new { Value = x.Value != null ? x.Value.AttemptedValue : "", errors = x.Errors.Select(y => y.ErrorMessage) }) }));
            }
            catch (Exception error)
            {
                SF.LogError(error);
                return(Json(new { result = "error", message = error.Message }));
            }
        }