Advertisement
Guest User

Untitled

a guest
Apr 13th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Net.Mail;
  6. using MySql.Data.MySqlClient;
  7. using System.Web.UI;
  8. using System.IO;
  9.  
  10. public class RestorePswd
  11. {
  12. public static void CanRestore(string username, string email, Page page)
  13. {
  14. if (username == "" || email == "")
  15. {
  16. SweetAlertCustom.SendCustomAlert(page, "Please fill all the boxes.", "ALERT!", "OK", SweetAlertCustom.types.error);
  17. return;
  18. }
  19. if (username.Length > 15 || email.Length > 40)
  20. {
  21. SweetAlertCustom.SendCustomAlert(page, "Wrong input.", "ALERT!", "OK", SweetAlertCustom.types.error);
  22. return;
  23. }
  24. string path = Constants.GlobalAccountsPath + username + ".txt";
  25. if (File.Exists(path))
  26. {
  27. string Data = File.ReadAllText(path);
  28. if (Data == email)
  29. {
  30. using (SmtpClient client = new SmtpClient())
  31. {
  32. client.Host = "smtp.gmail.com";
  33. client.Port = 587;
  34. client.EnableSsl = true;
  35. client.DeliveryMethod = SmtpDeliveryMethod.Network;
  36. client.Credentials = new System.Net.NetworkCredential("dukepaypal97@gmail.com", "bbmmtmi99Z");
  37. client.Timeout = 600000;
  38. using (MailMessage mm = new MailMessage("dukepaypal97@gmail.com", email, "Password Recovery", GetRecoveryContent(username)))
  39. client.Send(mm);
  40. }
  41. SweetAlertCustom.SendCustomAlert(page, "Your new password has been sent on your email.", "SUCCESS!", "OK", SweetAlertCustom.types.success);
  42. }
  43. }
  44. }
  45. public static bool ValidToken(string token, string username)
  46. {
  47. string path = Constants.PasswordRecoverPath + username + ".txt";
  48. if (!File.Exists(path))
  49. return false;
  50. string[] data = File.ReadAllLines(path);
  51. if (data[0] != token)
  52. return false;
  53. if (DateTime.FromBinary(long.Parse(data[1])) > DateTime.Now.AddHours(24))
  54. {
  55. File.Delete(path);
  56. return false;
  57. }
  58. return true;
  59. }
  60. static char[] Alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz".ToArray();
  61. static Random rnd = new Random();
  62. public static string RandomToken()
  63. {
  64. string ps = "";
  65. for (int i = 0; i < 50; i++)
  66. ps += Alphabets[rnd.Next(0, Alphabets.Length)];
  67. return ps;
  68. }
  69. public static string GetRecoveryContent(string username)
  70. {
  71. string path = Constants.PasswordRecoverPath + username + ".txt";
  72. string Token = RandomToken();
  73. if (File.Exists(path))
  74. File.Delete(path);
  75. string[] textlines = new string[2];
  76. textlines[0] = Token;
  77. textlines[1] = DateTime.Now.ToBinary().ToString();
  78. File.WriteAllLines(path, textlines);
  79. string link = "http://164.132.192.3/reset?token=" + Token + "&user=" + username;
  80. string content = "Hello " + username + ",\n\nYou have requested a password recovery link!\n\nPlease follow this link and enter your new password.\n\n" + link + "\nThis link will expire after 24 hours from now!\n\nThanks,\nPureClassic Team.";
  81. return content;
  82. }
  83. public static void DirectChange(string username, string newpass, Page page)
  84. {
  85. if (username == "" || newpass == "") page.Response.Redirect("Home");
  86. string path = Constants.GlobalAccountsPath + username + ".usr";
  87. if (File.Exists(path))
  88. File.Delete(path);
  89. MemoryStream ms = new MemoryStream();
  90. BinaryWriter BW = new BinaryWriter(ms);
  91. BW.Write(newpass);
  92. BW.Write("");
  93. byte[] buffer = ms.ToArray();
  94. BW.Close();
  95. ms.Close();
  96. File.WriteAllBytes(Constants.GlobalAccountsPath + username + ".usr", buffer);
  97. File.Delete(Constants.PasswordRecoverPath + username + ".txt");
  98. SweetAlertCustom.SendCustomAlert(page, "Your password has changed successfully!.", "Success", "COOL", SweetAlertCustom.types.success);
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement