コード例 #1
0
        public ActionResult UpdatePassword(string action, string oldpassword, string password, int iduser)
        {
            try
            {
                if (action == "cancel")
                {
                    //cancel
                    return(Json(new { valor = 0, href = Url.Action("Login", "Account") }));
                }
                else
                {
                    //guardar
                    if (!_tokenManager.GenerateToken("pepe", EncryptHelper.CalculateMD5Hash(password)))
                    {
                        ModelState.AddModelError("", Resources.LoginResources.InvalidUserPassword);
                    }

                    var userApi = new Spartan_UserApiConsumer();
                    userApi.SetAuthHeader(_tokenManager.Token);

                    var tmpuser = userApi.GetByKey(iduser, false);

                    if (tmpuser != null && tmpuser.Resource != null)
                    {
                        if (oldpassword == password)
                        {
                            return(Json(new { valor = 2, message = Resources.LoginResources.ErrorNewPassword }));
                        }

                        tmpuser.Resource.Password = EncryptHelper.CalculateMD5Hash(password);
                        var rta = userApi.Update(tmpuser.Resource, null, null);

                        var userhistApi = new Spartan_User_Historical_PasswordApiConsumer();
                        userhistApi.SetAuthHeader(_tokenManager.Token);

                        rta = userhistApi.Insert(new Core.Domain.Spartan_User_Historical_Password.Spartan_User_Historical_Password()
                        {
                            Fecha_de_Registro = DateTime.Now,
                            Usuario           = iduser,
                            Password          = EncryptHelper.CalculateMD5Hash(password)
                        }, null, null);

                        return(Json(new { valor = 1, href = Url.Action("Login", "Account") }));
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            catch (Exception ex)
            {
                //error
                return(null);
            }
        }
コード例 #2
0
        public JsonResult ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Spartan_User_Core UserDetails = _IUseroApiConsumer.ValidateUser(1, 10, "Username = '******'  COLLATE SQL_Latin1_General_CP1_CS_AS And Email = '" + model.Email + "'").Resource;
                    if (UserDetails.Spartan_Users != null && UserDetails.Spartan_Users.Count() > 0)
                    {
                        if (System.IO.File.Exists(Server.MapPath("~/HTMLTemplates/ForgotPassword.html")))
                        {
                            // Get HTML Template for Forgot password
                            StreamReader sread           = new StreamReader(Server.MapPath("~/HTMLTemplates/ForgotPassword.html"));
                            string       strBodyTemplate = sread.ReadToEnd();
                            // Replace User Full Name
                            strBodyTemplate = strBodyTemplate.Replace("*|fullname|*", UserDetails.Spartan_Users[0].Name);
                            strBodyTemplate = strBodyTemplate.Replace("*|username|*", UserDetails.Spartan_Users[0].Username);
                            strBodyTemplate = strBodyTemplate.Replace("*|email|*", UserDetails.Spartan_Users[0].Email);
                            //strBodyTemplate = strBodyTemplate.Replace("*|password|*", UserDetails.Spartan_Users[0].Password);


                            //Replace text for apropiates values in Resources
                            strBodyTemplate = strBodyTemplate.Replace("*|text1|*", Resources.LoginResources.Hello);
                            strBodyTemplate = strBodyTemplate.Replace("*|text2|*", Resources.LoginResources.textTemplateEmail1);
                            strBodyTemplate = strBodyTemplate.Replace("*|text3|*", Resources.LoginResources.UserName.ToString());
                            strBodyTemplate = strBodyTemplate.Replace("*|text4|*", Resources.LoginResources.Email.ToString());
                            strBodyTemplate = strBodyTemplate.Replace("*|text5|*", Resources.LoginResources.Password);
                            strBodyTemplate = strBodyTemplate.Replace("*|text6|*", Resources.LoginResources.textTemplateEmail2.ToString());


                            if (!_tokenManager.GenerateToken("admin", "admin"))
                            {
                                return(null);
                            }

                            var userApi = new Spartan_UserApiConsumer();
                            userApi.SetAuthHeader(_tokenManager.Token);
                            var tmpuser = userApi.GetByKey(UserDetails.Spartan_Users[0].Id_User, false);
                            var pass    = System.Web.Security.Membership.GeneratePassword(7, 0);
                            pass = Regex.Replace(pass, @"[^a-zA-Z0-9]", m => "9") + "$";
                            tmpuser.Resource.Password = EncryptHelper.CalculateMD5Hash(pass);
                            var res = userApi.Update(tmpuser.Resource, null, null);

                            var userhistApi = new Spartan_User_Historical_PasswordApiConsumer();
                            userhistApi.SetAuthHeader(_tokenManager.Token);

                            res = userhistApi.Insert(new Core.Domain.Spartan_User_Historical_Password.Spartan_User_Historical_Password()
                            {
                                Fecha_de_Registro = DateTime.Now,
                                Usuario           = tmpuser.Resource.Id_User,
                                Password          = tmpuser.Resource.Password
                            }, null, null);

                            strBodyTemplate = strBodyTemplate.Replace("*|password|*", pass);

                            // Replace ForgotPassword Link with Token and Encrypted Email
                            List <string> emails = new List <string>();
                            emails.Add(model.Email);
                            if (Helper.SendEmail(emails, string.Format(Resources.LoginResources.ForgotPasswordEmailSubject, model.UserName), strBodyTemplate))
                            {
                                return(Json(new { data = string.Format(Resources.LoginResources.ForgotPasswordSuccess, model.Email), valid = true }));
                            }
                            else
                            {
                                return(Json(new { data = Resources.LoginResources.ForgotPasswordEmailError, valid = false }));
                            }
                        }
                        else
                        {
                            return(Json(new { data = Resources.LoginResources.ForgotPasswordEmailError, valid = false }));
                        }
                    }
                    else
                    {
                        return(Json(new { data = Resources.LoginResources.InvalidEmailUserName, valid = false }));
                    }
                }
                catch (Exception)
                {
                    return(Json(new { data = Resources.LoginResources.InvalidEmailUserName, valid = false }));
                }
            }
            else
            {
                return(Json(Resources.LoginResources.InvalidEmailUserName));
            }
        }