public SimpleMembershipInitializer()
            {
                Database.SetInitializer<Entities>(null);

                try
                {
                    using (var context = new AgroGanSite.Models.Entities())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    if (!WebSecurity.Initialized)
                    {
                        WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
                    }
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
        public ActionResult ResetPassword(string un, string rt)
        {
            Entities db = new Entities();
            //TODO: Check the un and rt matching and then perform following
            //get userid of received username
            var userid = (from i in db.UserProfiles
                          where i.UserName == un
                          select i.UserId).FirstOrDefault();
            //check userid and token matches
            bool any = (from j in db.webpages_Memberships
                        where (j.UserId == userid)
                        && (j.PasswordVerificationToken == rt)
                        //&& (j.PasswordVerificationTokenExpirationDate < DateTime.Now)
                        select j).Any();

            if (any == true)
            {
                //generate random password
                string newpassword = GenerateRandomPassword(6);
                //reset password
                bool response = WebSecurity.ResetPassword(rt, newpassword);
                if (response == true)
                {
                    //get user emailid to send password
                    var emailid = (from i in db.UserProfiles
                                   where i.UserName == un
                                   select i.EmailId).FirstOrDefault();
                    //send email
                    string subject = "Nueva contraseña";
                    string body = "<b>Su nueva contraseña creada:</b><br/>" + newpassword; //edit it
                    try
                    {
                        SendEMail(emailid, subject, body);
                        TempData["Message"] = "Correo enviado.";
                    }
                    catch (Exception ex)
                    {
                        TempData["Message"] = "Ha ocurrido un error mientras se enviaba el corroe." + ex.Message;
                    }

                    //display message
                    TempData["Message"] = "Exito! Verifique su correo electrónico. Su nueva contraseña es: " + newpassword;
                }
                else
                {
                    TempData["Message"] = "Hey, avoid random request on this page.";
                }
            }
            else
            {
                TempData["Message"] = "El usuario y el tokem no son válidos-";
            }

            return View();
        }
        public ActionResult ForgotPassword(string UserName)
        {
            //check user existance
            var user = Membership.GetUser(UserName);
            if (user == null)
            {
                TempData["Message"] = "El usuario no existe.";
            }
            else
            {
                //generate password token
                var token = WebSecurity.GeneratePasswordResetToken(UserName);
                //create url with above token
                var resetLink = "<a href='" + Url.Action("ResetPassword", "Account", new { un = UserName, rt = token }, "http") + "'>Restaurar contraseña</a>";
                //get user emailid
                Entities db = new Entities();
                var emailid = (from i in db.UserProfiles
                               where i.UserName == UserName
                               select i.EmailId).FirstOrDefault();
                //send mail
                string subject = "Por favor proceda a actualizar ssu password";
                string body = "<b>En este link prodrá restaurar su contraseña</b><br/>" + resetLink; //edit it
                try
                {
                    SendEMail(emailid, subject, body);
                    TempData["Message"] = "Mensaje enviado.";
                }
                catch (Exception ex)
                {
                    TempData["Message"] = "Ha ocurrido un error enviando el mensaje." + ex.Message;
                }
                //only for testing
                TempData["Message"] = "Se ha enviado un correo electrónico donde podrá restaurar sus credenciales.";
            }

            return View();
        }
Ejemplo n.º 4
0
 public ActionResult ImagesChooser()
 {
     Entities db = new Entities();
     var allimages = db.IMG_Images.ToList();
     return PartialView("ImagesChooser", allimages);
 }