/Notas/Notas/Clases/Utilities.cs

https://gitlab.com/cealer/Notas · C# · 216 lines · 179 code · 33 blank · 4 comment · 12 complexity · 21109cba00cf7273e16d0fba51b3ff9d MD5 · raw file

  1. using Microsoft.AspNet.Identity;
  2. using Microsoft.AspNet.Identity.EntityFramework;
  3. using Notas.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Mail;
  10. using System.Threading.Tasks;
  11. using System.Web;
  12. using System.Web.Configuration;
  13. namespace Notas.Clases
  14. {
  15. public class Utilities : IDisposable
  16. {
  17. //La tabla creada al crear el proyectos
  18. private static ApplicationDbContext userContext = new ApplicationDbContext();
  19. //Mis tablas
  20. private static NotesContext db = new NotesContext();
  21. public static void CheckRole(string roleName)
  22. {
  23. var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(userContext));
  24. // Check to see if Role Exists, if not create it
  25. if (!roleManager.RoleExists(roleName))
  26. {
  27. roleManager.Create(new IdentityRole(roleName));
  28. }
  29. }
  30. public static void CheckSuperUser(string role)
  31. {
  32. var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(userContext));
  33. var email = WebConfigurationManager.AppSettings["AdminUser"];
  34. var password = WebConfigurationManager.AppSettings["AdminPassWord"];
  35. var userASP = userManager.FindByName(email);
  36. if (userASP == null)
  37. {
  38. CreateUserASP(email, role, password);
  39. return;
  40. }
  41. }
  42. public static void CreateUserASP(string email)
  43. {
  44. var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(userContext));
  45. var userASP = new ApplicationUser
  46. {
  47. Email = email,
  48. UserName = email,
  49. };
  50. userManager.Create(userASP, email);
  51. }
  52. public static void CreateUserASP(string email, string roleName)
  53. {
  54. var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(userContext));
  55. var userASP = new ApplicationUser
  56. {
  57. Email = email,
  58. UserName = email,
  59. };
  60. userManager.Create(userASP, email);
  61. userManager.AddToRole(userASP.Id, roleName);
  62. }
  63. public static void AddRoleToUser(string email, string roleName)
  64. {
  65. var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(userContext));
  66. var userASP = userManager.FindByEmail(email);
  67. if (userASP == null)
  68. {
  69. return;
  70. }
  71. userManager.AddToRole(userASP.Id, roleName);
  72. }
  73. public static void CreateUserASP(string email, string roleName, string password)
  74. {
  75. var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(userContext));
  76. var userASP = new ApplicationUser
  77. {
  78. Email = email,
  79. UserName = email,
  80. };
  81. userManager.Create(userASP, password);
  82. userManager.AddToRole(userASP.Id, roleName);
  83. }
  84. public static async Task SendMail(string to, string subject, string body)
  85. {
  86. var message = new MailMessage();
  87. message.To.Add(new MailAddress(to));
  88. message.From = new MailAddress(WebConfigurationManager.AppSettings["AdminUser"]);
  89. message.Subject = subject;
  90. message.Body = body;
  91. message.IsBodyHtml = true;
  92. using (var smtp = new SmtpClient())
  93. {
  94. var credential = new NetworkCredential
  95. {
  96. UserName = WebConfigurationManager.AppSettings["AdminUser"],
  97. Password = WebConfigurationManager.AppSettings["AdminPassWord"]
  98. };
  99. smtp.Credentials = credential;
  100. smtp.Host = WebConfigurationManager.AppSettings["SMTPName"];
  101. smtp.Port = int.Parse(WebConfigurationManager.AppSettings["SMTPPort"]);
  102. smtp.EnableSsl = true;
  103. await smtp.SendMailAsync(message);
  104. }
  105. }
  106. public static async Task SendMail(List<string> mails, string subject, string body)
  107. {
  108. var message = new MailMessage();
  109. foreach (var to in mails)
  110. {
  111. message.To.Add(new MailAddress(to));
  112. }
  113. message.From = new MailAddress(WebConfigurationManager.AppSettings["AdminUser"]);
  114. message.Subject = subject;
  115. message.Body = body;
  116. message.IsBodyHtml = true;
  117. using (var smtp = new SmtpClient())
  118. {
  119. var credential = new NetworkCredential
  120. {
  121. UserName = WebConfigurationManager.AppSettings["AdminUser"],
  122. Password = WebConfigurationManager.AppSettings["AdminPassWord"]
  123. };
  124. smtp.Credentials = credential;
  125. smtp.Host = WebConfigurationManager.AppSettings["SMTPName"];
  126. smtp.Port = int.Parse(WebConfigurationManager.AppSettings["SMTPPort"]);
  127. smtp.EnableSsl = true;
  128. await smtp.SendMailAsync(message);
  129. }
  130. }
  131. public static async Task PasswordRecovery(string email)
  132. {
  133. var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(userContext));
  134. var userASP = userManager.FindByEmail(email);
  135. if (userASP == null)
  136. {
  137. return;
  138. }
  139. var user = db.Users.Where(tp => tp.UserName == email).FirstOrDefault();
  140. if (user == null)
  141. {
  142. return;
  143. }
  144. var random = new Random();
  145. var newPassword = string.Format("{0}{1}{2:04}*",
  146. user.FirstName.Trim().ToUpper().Substring(0, 1),
  147. user.LastName.Trim().ToLower(),
  148. random.Next(10000));
  149. userManager.RemovePassword(userASP.Id);
  150. userManager.AddPassword(userASP.Id, newPassword);
  151. var subject = "Notes Password Recovery";
  152. var body = string.Format(@"
  153. <h1>Taxes Password Recovery</h1>
  154. <p>Yor new password is: <strong>{0}</strong></p>
  155. <p>Please change it for one, that you remember easyly",
  156. newPassword);
  157. await SendMail(email, subject, body);
  158. }
  159. public static string UploadPhoto(HttpPostedFileBase file)
  160. {
  161. // Upload image
  162. string path = string.Empty;
  163. string pic = string.Empty;
  164. if (file != null)
  165. {
  166. pic = Path.GetFileName(file.FileName);
  167. path = Path.Combine(HttpContext.Current.Server.MapPath("~/Content/Photos"), pic);
  168. file.SaveAs(path);
  169. using (MemoryStream ms = new MemoryStream())
  170. {
  171. file.InputStream.CopyTo(ms);
  172. byte[] array = ms.GetBuffer();
  173. }
  174. }
  175. return pic;
  176. }
  177. public void Dispose()
  178. {
  179. userContext.Dispose();
  180. db.Dispose();
  181. }
  182. }
  183. }