// GET: restablecerPassword public ActionResult Index(string id, string llave) { Session["user_usuario"] = null; Session.Abandon(); Session.Clear(); Session.RemoveAll(); int?buscaUsuario = context.GetUsuarioEncriptado(id).FirstOrDefault(); if (buscaUsuario != null) { icb_solicitudnuevaclave buscarFechaSolicitud = context.icb_solicitudnuevaclave.FirstOrDefault(x => x.solicitud_llave == llave); if (buscarFechaSolicitud != null) { TimeSpan diff = DateTime.Now - buscarFechaSolicitud.solicitud_fecha; double hours = diff.TotalHours; if (hours > 0 && hours < 72) { users usuario = context.users.FirstOrDefault(x => x.user_id == buscaUsuario); if (usuario != null && usuario.user_estado) { CambioContrasenaModel modelo = new CambioContrasenaModel { id_usuario = usuario.user_id }; return(View(modelo)); } return(RedirectToAction("Error")); } return(RedirectToAction("Error")); } } return(RedirectToAction("Error")); }
public JsonResult RecordarContrasena(string nombreUsuario) { configuracion_envio_correos correoconfig = context.configuracion_envio_correos.Where(d => d.activo).FirstOrDefault(); bool guardar = false; users buscaUsuario = context.users.FirstOrDefault(x => x.user_usuario == nombreUsuario); if (buscaUsuario != null) { if (buscaUsuario.user_estado) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); md5.ComputeHash(Encoding.ASCII.GetBytes(nombreUsuario)); byte[] result = md5.Hash; StringBuilder str = new StringBuilder(); for (int i = 0; i < result.Length; i++) { str.Append(result[i].ToString("x2")); } Random rdm = new Random(); int aleatorio = rdm.Next(1, 1000); string llave = DateTime.Now.ToString() + aleatorio; md5.ComputeHash(Encoding.ASCII.GetBytes(llave)); byte[] resultLlave = md5.Hash; StringBuilder strLlave = new StringBuilder(); for (int i = 0; i < resultLlave.Length; i++) { strLlave.Append(resultLlave[i].ToString("x2")); } icb_solicitudnuevaclave crearLlave = context.icb_solicitudnuevaclave.Add(new icb_solicitudnuevaclave { solicitud_fecha = DateTime.Now, solicitud_llave = strLlave.ToString(), solucitud_usuario = nombreUsuario }); bool guardarLlave = context.SaveChanges() > 0; if (guardarLlave) { //Esto es para enviar el correo, temporalmente se esta guardando en base de datos para probar el enlace string urlActual0 = Request.Url.Scheme + "://" + Request.Url.Authority; //var lastFolder = Path.GetDirectoryName(urlActual0); //var pathWithoutLastFolder = Path.GetDirectoryName(lastFolder); string url = urlActual0 += @"/restablecerPassword/Index?id=" + str + "&&llave=" + strLlave; string body = url; context.envio_correo_test.Add(new envio_correo_test { correo_envio = buscaUsuario.user_email, usuario = nombreUsuario, body = body }); } guardar = context.SaveChanges() > 0; try { var enlace = (from n in context.icb_solicitudnuevaclave join e in context.envio_correo_test on n.solicitud_id equals e.id_tabla orderby n.solicitud_fecha descending where e.correo_envio == buscaUsuario.user_email select new { e.body }).FirstOrDefault(); string enlaces = enlace.body.Replace("<a href='", ""); enlaces = enlaces.Replace("'></a>", ""); int aniocorreo = DateTime.Now.Year; users user_destinatario = context.users.Find(buscaUsuario.user_id); //var user_remitente = context.users.Find(usuario_actual); //MailAddress de = new MailAddress("*****@*****.**", "Notificación Iceberg"); MailAddress de = new MailAddress(correoconfig.correo, correoconfig.nombre_remitente); MailAddress para = new MailAddress(user_destinatario.user_email, user_destinatario.user_nombre + " " + user_destinatario.user_apellido); MailMessage mensaje = new MailMessage(de, para); //texto del mensaje de correo string titulocorreo = "Restauración de Contraseña"; string asuntocorreo = "<p>Hemos recibido una solicitud para reestablecer tu contraseña, por favor haz click en el siguiente boton</p><br />"; string textoinicial = ""; textoinicial += "<a style='background-color: #1A6AA2; color: white; padding: 7px 12px; text-align: center; text-decoration: none; display: inline-block;' href=" + enlaces + ">Reestablecer</a>"; textoinicial += "<p>Por motivos de seguridad, el vinculo caducará 72 horas despues de su envío.</p>"; string html2 = @"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> <title>Notificacion Iceberg Email</title> <style type='text/css'> body {margin: 0; padding: 0; min-width: 100%!important;} img {height: auto;} .content {width: 100%; max-width: 600px;} .header {padding: 40px 30px 20px 30px;} .innerpadding {padding: 30px 30px 30px 30px;} .borderbottom {border-bottom: 1px solid #f2eeed;} .subhead {font-size: 15px; color: #ffffff; font-family: sans-serif; letter-spacing: 10px;} .h1, .h2, .bodycopy {color: #153643; font-family: sans-serif;} .h1 {font-size: 33px; line-height: 38px; font-weight: bold;} .h2 {padding: 0 0 15px 0; font-size: 24px; line-height: 28px; font-weight: bold;} .bodycopy {font-size: 16px; line-height: 22px;} .button {text-align: center; font-size: 18px; font-family: sans-serif; font-weight: bold; padding: 0 30px 0 30px;} .button a {color: #ffffff; text-decoration: none;} .footer {padding: 20px 30px 15px 30px;} .footercopy {font-family: sans-serif; font-size: 14px; color: #ffffff;} .footercopy a {color: #ffffff; text-decoration: underline;} .unsubscribe {display: block; margin-top: 20px; padding: 10px 50px; background: #2f3942; border-radius: 5px; text-decoration: none!important; font-weight: bold;} .hide {display: none!important;} @media only screen and (max-width: 550px), screen and (max-device-width: 550px) { body[yahoo] .hide {display: none!important;} body[yahoo] .buttonwrapper {background-color: transparent!important;} body[yahoo] .button {padding: 0px!important;} body[yahoo] .button a {background-color: #e05443; padding: 15px 15px 13px!important;} body[yahoo] .unsubscribe {display: block; margin-top: 20px; padding: 10px 50px; background: #2f3942; border-radius: 5px; text-decoration: none!important; font-weight: bold;} } </style> </head> <body yahoo=yahoo bgcolor='#ffffff'> <img src='cid:logoiceberg' alt='Alternate Text' style='width: 150px'/> <br/> <table width='600px' bgcolor='#ffffff' border='0' cellpadding='0' cellspacing='0'> <tr> <td class='innerpadding borderbottom'> <table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr> <td class='bodycopy'><b>" + titulocorreo + @"</b> </td> </tr> <tr> <td class='bodycopy'> <b> Hola, " + user_destinatario.user_nombre + " " + user_destinatario.user_apellido + @"</b><br/> </td> </tr> </table> </td> </tr> <tr> <td class='innerpadding borderbottom'> <table width='115' align='left' border='0' cellpadding='0' cellspacing='0'> </table> <!--[if (gte mso 9)|(IE)]> <table width='420' align='left' cellpadding='0' cellspacing='0' border='0'> <tr> <td> <![endif]--> <table class='col380' align='left' border='0' cellpadding='0' cellspacing='0' style='width: 100%; max-width: 420px;'> <tr> <td> <table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr> <td class='bodycopy'> <p>" + asuntocorreo + @"</p> <p>" + textoinicial + @"</p> </td> </tr> </table> </td> </tr> </table> <!--[if (gte mso 9)|(IE)]> </td> </tr> </table> <![endif]--> </td> </tr> <tr> <td class='footer' bgcolor='#09599C'> <table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr> <td align='center' class='footercopy'> ® ICEBERG, " + aniocorreo + @"<br /> </td> </tr> </table> </td> </tr> </table> <!--[if (gte mso 9)|(IE)]> </td> </tr> </table> <![endif]--> </td> </tr> </table> </body> </html> "; //mensaje.Bcc.Add("*****@*****.**"); mensaje.Bcc.Add("*****@*****.**"); //mensaje.Bcc.Add("*****@*****.**"); mensaje.Subject = "Solicitud cambio de contraseña"; //mensaje.ReplyToList.Add("*****@*****.**"); mensaje.BodyEncoding = Encoding.Default; mensaje.IsBodyHtml = true; LinkedResource theEmailImage6 = new LinkedResource(HostingEnvironment.MapPath("~/Images/icebergerp03.png"), "image/png") { ContentId = "logoiceberg", TransferEncoding = TransferEncoding.Base64 }; theEmailImage6.ContentType.Name = theEmailImage6.ContentId; theEmailImage6.ContentLink = new Uri("cid:" + theEmailImage6.ContentId); //create Alrternative HTML view //string body = string.Format(html2, theEmailImage.ContentId, theEmailImage2.ContentId, theEmailImage3.ContentId, theEmailImage4.ContentId); AlternateView htmlView = AlternateView.CreateAlternateViewFromString(html2, null, "text/html"); htmlView.LinkedResources.Add(theEmailImage6); mensaje.AlternateViews.Add(htmlView); //mensaje.Body = html2; //set the Email subject mensaje.Subject = "Recuperar Contraseña Iceberg"; //var html = ""; //var a = Request.Url.Scheme + "://" + Request.Url.Authority + "/Images/icebergerp03.png"; //html += "<img src='"+a+"' width='90' height='56'/>"; //html += "<h4>Reestauracion de contraseña</h4>"; //html += "<p>Hola "+ user_destinatario.user_nombre + " " + user_destinatario.user_apellido + "</p><br>"; //html += "<p>Hemos recibido un solicitud para reestablecer tu contraseña, por favor da click en el siguiente boton</p><br />"; //html += "<a style='background-color: #1A6AA2; color: white; padding: 14px 25px; text-align: center; text-decoration: none; display: inline-block;' href=" +enlaces+ ">Reestablecer</a>"; //html += "<p>Por motivos de seguridad, el vinculo caducara 72 horas despues de su envio.</p>"; //mensaje.Body = html; //SmtpClient cliente = new SmtpClient("smtp.mi.com.co"); SmtpClient cliente = new SmtpClient(correoconfig.smtp_server) { Port = correoconfig.puerto, UseDefaultCredentials = false, Credentials = new NetworkCredential(correoconfig.usuario, correoconfig.password), EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network }; cliente.Send(mensaje); var data = new { tipo = "success", mensaje = "Mensaje enviado exitosamente" }; return(Json(data, JsonRequestBehavior.AllowGet)); // procesado = true; } catch (Exception ex) { string error = ex.Message; var data = new { tipo = "error", mensaje = error }; return(Json(data, JsonRequestBehavior.AllowGet)); } } { var data = new { tipo = "error", mensaje = "El usuario se encuentra bloqueado. Por favor contacte con el administrador del sistema" }; return(Json(data, JsonRequestBehavior.AllowGet)); } } else { var data = new { tipo = "error", mensaje = "No existe un usuario con el nombre de usuario suministrado. Por favor valide" }; return(Json(data, JsonRequestBehavior.AllowGet)); } //return Json(guardar, JsonRequestBehavior.AllowGet); }