Ejemplo n.º 1
0
        public ActionResult ResetPassword(ResetPasswordModel model)
        {
            if (ModelState.IsValid)
            {
                using (AddressBookEntitiesForAccountModel dc = new AddressBookEntitiesForAccountModel())
                {
                    var v = dc.Users.Where(a => a.Email.Equals(model.ReturnToken) && a.resetPassword.Equals(model.TempPassword)).FirstOrDefault();
                    if (v != null)
                    {
                        v.PasswordHashed = AddressBook.Cyrpto.Hash(model.Password);
                        v.resetPassword  = null;
                        dc.SaveChanges();
                        ViewBag.Message               = "Password Successfully Reset";
                        Session["LogedUserID"]        = v.Id.ToString();
                        Session["LogedUserFirstName"] = v.FirstName.ToString();
                        var lName = v != null?Convert.ToString(v.LastName) : null;

                        if (lName != null)
                        {
                            Session["LogedUserLastName"] = v.LastName.ToString();
                        }

                        @Session["IsAdmin"] = v.IsAdmin.ToString();

                        return(RedirectToAction("AfterLogin"));
                    }
                    else
                    {
                        ViewBag.Message = "We can't find you!";
                    }
                }
            }
            return(View(model));
        }
Ejemplo n.º 2
0
        public ActionResult ForgotPassword(ForgotPasswordModel model)
        {
            using (AddressBookEntitiesForAccountModel dc = new AddressBookEntitiesForAccountModel())
            {
                var v = dc.Users.Where(a => a.Email.Equals(model.Email)).FirstOrDefault();
                if (v != null)
                {
                    // Generae password token that will be used in the email link to authenticate user
                    var token = AddressBook.Cyrpto.RandomString();
                    // Generate the html link sent via email
                    string html = "<!DOCTYPE html><html lang=" + "en" + "> <head><meta charset=" + "UTF-8"
                                  + "><title>Title</title> </head> <body> <p>Your temporary password : <span>" + token + "</span></p> </body> </html>";
                    string resetLink = html + "<a href='"
                                       + Url.Action("ResetPassword", "Account", new { rt = model.Email }, "http")
                                       + "'>Reset Password Link</a>";

                    // Email stuff
                    string subject = "Reset your password for IDC Address Book";
                    string body    = resetLink;
                    string from    = "*****@*****.**";

                    MailMessage message = new MailMessage(from, model.Email);
                    message.Subject    = subject;
                    message.Body       = body;
                    message.IsBodyHtml = true;
                    SmtpClient client = new SmtpClient();

                    // Attempt to send the email
                    try
                    {
                        client.Send(message);
                        v.resetPassword = token;
                        dc.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        ModelState.AddModelError("", "Issue sending email: " + e.Message);
                    }
                }
                else
                {
                    /* Note: You may not want to provide the following information
                     * since it gives an intruder information as to whether a
                     * certain email address is registered with this website or not.
                     * If you're really concerned about privacy, you may want to
                     * forward to the same "Success" page regardless whether an
                     * user was found or not. This is only for illustration purposes.
                     */
                    ModelState.AddModelError("", "No user found by that email.");
                }
                ViewBag.Message = "Reset Password link sent to your email";
            }


            /* You may want to send the user to a "Success" page upon the successful
             * sending of the reset email link. Right now, if we are 100% successful
             * nothing happens on the page. :P
             */
            return(View(model));
        }
Ejemplo n.º 3
0
 public ActionResult AddNewUsers(User u)
 {
     if (ModelState.IsValid)
     {
         using (AddressBookEntitiesForAccountModel dc = new AddressBookEntitiesForAccountModel())
         {
             string hashPass = AddressBook.Cyrpto.Hash(u.PasswordHashed);
             u.PasswordHashed = hashPass;
             dc.Users.Add(u);
             try
             {
                 dc.SaveChanges();
             }
             catch (System.Data.Entity.Validation.DbEntityValidationException e)
             {
                 foreach (var eve in e.EntityValidationErrors)
                 {
                     Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                       eve.Entry.Entity.GetType().Name, eve.Entry.State);
                     foreach (var ve in eve.ValidationErrors)
                     {
                         Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                           ve.PropertyName, ve.ErrorMessage);
                     }
                 }
             }
         }
         return(RedirectToAction("ViewUsers"));
     }
     else
     {
         return(View(u));
     }
 }
Ejemplo n.º 4
0
 public ActionResult EditMyProfile(UserValidation u)
 {
     if (ModelState.IsValid)
     {
         using (AddressBookEntitiesForAccountModel dc = new AddressBookEntitiesForAccountModel())
         {
             string username = User.Identity.Name;
             var    v        = dc.Users.Where(a => a.Id.Equals(u.Id)).FirstOrDefault();
             if (v != null)
             {
                 Session["LogedUserFirstName"] = u.FirstName.ToString();
                 v.FirstName      = u.FirstName;
                 v.LastName       = u.LastName;
                 v.Email          = u.Email;
                 v.PasswordHashed = u.PasswordHashed;
             }
             dc.SaveChanges();
         }
         return(RedirectToAction("Index"));
     }
     else
     {
         List <UserValidation> user = new List <UserValidation>();
         user.Add(u);
         ViewBag.userdetails = user;
         return(View());
     }
 }
Ejemplo n.º 5
0
 public ActionResult EditUsers(User u)
 {
     if (ModelState.IsValid)
     {
         using (AddressBookEntitiesForAccountModel dc = new AddressBookEntitiesForAccountModel())
         {
             var v = dc.Users.Where(a => a.Id.Equals(u.Id)).FirstOrDefault();
             if (v != null)
             {
                 v.Id             = u.Id;
                 v.FirstName      = u.FirstName;
                 v.LastName       = u.LastName;
                 v.Email          = u.Email;
                 v.PasswordHashed = u.PasswordHashed;
             }
             dc.SaveChanges();
         }
         return(RedirectToAction("ViewUsers"));
     }
     else
     {
         List <User> user = new List <User>();
         user.Add(u);
         ViewBag.userdetails = user;
         return(View());
     }
 }