public async Task<JsonData> ChangePassword(ChangePasswordModel changePass)
        {
            try
            {
                var db = new DataContext();
                var userMan = new UserManager<MyUser>(new UserStore<MyUser>(db));
                userMan.UserValidator = new UserValidator<MyUser>(userMan)
                {
                    AllowOnlyAlphanumericUserNames =
                        false
                };

                var user = await userMan.FindByIdAsync(User.Identity.GetUserId());
                if (user == null) throw new Exception("please check your old password");

                var newPassword = changePass.NewPassword;
                var result = await userMan.RemovePasswordAsync(user.Id);
                if (!result.Succeeded) throw new Exception(string.Join(", ", result.Errors));
                var result2 = await userMan.AddPasswordAsync(user.Id, newPassword);
                if (!result2.Succeeded) throw new Exception(string.Join(", ", result2.Errors));
                return DataHelpers.ReturnJsonData(null, true, "Password changed successful");
            }
            catch (Exception e)
            {
                return DataHelpers.ExceptionProcessor(e);
            }
        }
        public async Task<WikiDownUser> Save(IPrincipal principal, UserManager<WikiDownUser> userManager)
        {
            var user = await userManager.FindByNameAsync(this.UserName);

            var roles = this.GetRoles(principal, user);

            if (user != null)
            {
                if (user.UserName == principal.Identity.Name)
                {
                    var userAccessLevel = ArticleAccessHelper.GetAccessLevel(user.Roles);
                    if (userAccessLevel < ArticleAccessLevel.Admin)
                    {
                        throw new HttpResponseException(HttpStatusCode.BadRequest);
                    }
                }

                user.SetRoles(roles);
                user.SetDisplayName(this.DisplayName);
                user.SetEmail(this.Email);

                if (!string.IsNullOrWhiteSpace(this.Password))
                {
                    await userManager.RemovePasswordAsync(user.Id);
                    await userManager.AddPasswordAsync(user.Id, this.Password);
                }

                await userManager.UpdateAsync(user);

                WikiDownUserCacheHelper.Clear(user.UserName);
            }
            else
            {
                user = new WikiDownUser(this.UserName) { Roles = roles };
                user.SetDisplayName(this.DisplayName);
                user.SetEmail(this.Email);

                await userManager.CreateAsync(user, this.Password);
            }

            return user;
        }
 public async Task<JsonData> Reset(UserViewModel model)
 {
     try
     {
         var db = new DataContext();
         var userMan = new UserManager<MyUser>(new UserStore<MyUser>(db));
         userMan.UserValidator = new UserValidator<MyUser>(userMan)
         {
             AllowOnlyAlphanumericUserNames =
                 false
         };
         var user = await userMan.FindByEmailAsync(model.Email);
         if (user == null) throw new Exception("please check the email address");
         //todo: generate a unique password and email it to the user
         var newPassword = user.FullName.Substring(2, 3) + user.PasswordHash.Substring(0, 5);
         var result = await userMan.RemovePasswordAsync(user.Id);
         if (!result.Succeeded) throw new Exception(string.Join(", ", result.Errors));
         var result2 = await userMan.AddPasswordAsync(user.Id, newPassword);
         if (!result2.Succeeded) throw new Exception(string.Join(", ", result2.Errors));
         //todo: Email the new password to the user
         return DataHelpers.ReturnJsonData(null, true, "A new password has been emailed to your email address");
     }
     catch (Exception e)
     {
         return DataHelpers.ExceptionProcessor(e);
     }
 }
        public async Task<IHttpActionResult> ForgotPassword(ForgotPasswordModel model)
        {
              
            if (ModelState.IsValid)
            {
                
                // Fetch userID by email
                DBservices dbs = new DBservices();
                dbs = dbs.ReadFromDataBase(27, model.EmailAddress);
                string userId = dbs.dt.Rows[0].ItemArray[0].ToString();
                string userName = dbs.dt.Rows[0].ItemArray[1].ToString();
                string userFname = dbs.dt.Rows[0].ItemArray[2].ToString();
                
                // Generate an 8th digit long password
                var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
                var stringChars = new char[8];
                var random = new Random();
                for (int i = 0; i < stringChars.Length; i++)
                {
                    stringChars[i] = chars[random.Next(chars.Length)];
                }
                var randomPassword = new String(stringChars);
                UserManager<IdentityUser> UserManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>());
                IdentityResult resultRem = await UserManager.RemovePasswordAsync(userId);
                IdentityResult resultAdd = await UserManager.AddPasswordAsync(userId, randomPassword);

                //Send a notification to the user

                MailMessage mail = new MailMessage();
                StreamReader reader = new StreamReader(HttpContext.Current.Server.MapPath("~/Models/mailTemplates/forgotPasswordTemplate.html"));
                string readFile = reader.ReadToEnd();
                string StrContent = readFile;
                
                StrContent = StrContent.Replace("[FirstName]", userFname);
                StrContent = StrContent.Replace("[UserName]", userName);
                StrContent = StrContent.Replace("[Password]", randomPassword);
                
                mail.IsBodyHtml = true;
                mail.To.Add(model.EmailAddress);
                mail.Subject = "רוכבים לעבודה, איפוס סיסמא";                
                mail.Body = StrContent.ToString();
                mail.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Send(mail);

            }

            return Ok();

        }