public ActionResult ResetPassword(String token)
        {
            String password = Membership.GeneratePassword(10, 3);
            int userId = WebSecurity.GetUserIdFromPasswordResetToken(token);
            UserProfile userProfile = _repo.Get<UserProfile>(userId);
            MessageModel messageModel = new MessageModel();
            if (WebSecurity.ResetPassword(token, password))
            {
                IDictionary<string, string> data = new Dictionary<string, string>();
                data.Add("name", userProfile.Name);
                data.Add("password", password);
                String subject = String.Format("\"{0}\" - SPC VMS-Tufman Reconciliation - Reset password", userProfile.Name);
                String body = new TemplatingService().GetTemplatedDocument("NewPasswordEmail.vm", data);
                new EmailServices().SendEmail(subject, body, userProfile.Email);
            }
            else
            {
                messageModel.Title = "Error";
                messageModel.Body = "Your password could not be reseted. It may be because the link you used has already been used.";

                return View("Message",messageModel);
            }
            messageModel.Body = "An email has been sent with your new password";
            return View("Message", messageModel);
        }
        public ActionResult Register(RegisterModel model)
        {
            if(WebSecurity.UserExists(model.UserName))
                ModelState.AddModelError("Name", "This User name is already registered");
            if(model.SelectedRole.Equals(RoleList.Country) && String.IsNullOrEmpty(model.SelectedCountry))
                ModelState.AddModelError("SelectedCountry", "Select a country for this user");

            if (ModelState.IsValid)
            {
                try
                {
                    String password = Membership.GeneratePassword(10,0);
                    WebSecurity.CreateUserAndAccount(model.UserName, password, new { Country = model.SelectedCountry, Email = model.Email });
                    Roles.AddUserToRole(model.UserName, model.SelectedRole);
                    IDictionary<string, string> data = new Dictionary<string, string>();
                    data.Add("name",model.UserName);
                    data.Add("password", password);
                    String subject = String.Format("\"{0}\" - You can now connect to SPC VMS-Tufman Reconciliation ", model.UserName);
                    String body = new TemplatingService().GetTemplatedDocument("NewUserEmail.vm", data);
                    new EmailServices().SendEmail(subject, body, model.Email);
                    MessageModel messageModel = new MessageModel();
                    messageModel.Body = "An email has been sent to this user with its credentials";
                    return View("Message", messageModel);
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }
            // If we got this far, something failed, redisplay form
            model.Roles = Roles.GetAllRoles().Select(c => new SelectListItem { Value = c, Text = c }).ToList();
            model.Countries = _repo.GetAll<TufmanCountry>().Select(c => new SelectListItem { Value = c.Code, Text = c.Label }).ToList();
            return View(model);
        }
 public ActionResult RequestPassword(String email)
 {
     UserProfile userProfile = _repo.Find<UserProfile>(x => x.Email.Equals(email)).FirstOrDefault();
     MessageModel messageModel = new MessageModel();
     if (userProfile != null)
     {
         String token = WebSecurity.GeneratePasswordResetToken(userProfile.Name);
         IDictionary<string, string> data = new Dictionary<string, string>();
         data.Add("name", userProfile.Name);
         data.Add("token", token);
         String subject = String.Format("\"{0}\" - SPC VMS-Tufman Reconciliation - Reset password", userProfile.Name);
         String body = new TemplatingService().GetTemplatedDocument("ResetPasswordEmail.vm", data);
         new EmailServices().SendEmail(subject, body, email);
         messageModel.Body = "An email has been sent with a link to reset your password";
         return View("Message", messageModel);
     }
     messageModel.Title = "Unknown Email";
     messageModel.Body = String.Format("User with email {0} is not registered in the application", email);
     return View("Message", messageModel);
 }
        public ActionResult Manage(LocalPasswordModel model)
        {
            bool hasLocalAccount = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
            ViewBag.HasLocalPassword = hasLocalAccount;
            ViewBag.ReturnUrl = Url.Action("Manage");
            if (hasLocalAccount)
            {
                if (ModelState.IsValid)
                {
                    // ChangePassword will throw an exception rather than return false in certain failure scenarios.
                    bool changePasswordSucceeded;
                    try
                    {
                        changePasswordSucceeded = WebSecurity.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword);
                    }
                    catch (Exception)
                    {
                        changePasswordSucceeded = false;
                    }

                    if (changePasswordSucceeded)
                    {
                        MessageModel messageModel = new MessageModel();
                        messageModel.Body = "Password modified";
                        return View("Message", messageModel);
                    }
                    else
                    {
                        ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
                    }
                }
            }
            else
            {
                // User does not have a local password so remove any validation errors caused by a missing
                // OldPassword field
                ModelState state = ModelState["OldPassword"];
                if (state != null)
                {
                    state.Errors.Clear();
                }

                if (ModelState.IsValid)
                {
                    try
                    {
                        WebSecurity.CreateAccount(User.Identity.Name, model.NewPassword);
                        return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess });
                    }
                    catch (Exception e)
                    {
                        ModelState.AddModelError("", e);
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }