public JsonResult verifyCode(string code) { DateTime today = DateTime.Now.Date; VisitorReg reg = db.VisitorReg.FirstOrDefault(r => r.VisitorReg_code == code && r.VisitorReg_date == today); var result = new { Success = true }; if (reg == null) { result = new { Success = false }; } return(Json(result)); }
public JsonResult regVisitor(require require) { string email = require.email; if (db.VisitorReg.Any(v => v.VisitorReg_email == email)) { var fail = new { Success = "duplicate" }; return(Json(fail, JsonRequestBehavior.DenyGet)); } var result = new { Success = "true" }; //generate random code from http://stackoverflow.com/questions/1344221/how-can-i-generate-random-alphanumeric-strings-in-c var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var stringChars = new char[6]; var random = new Random(); for (int i = 0; i < stringChars.Length; i++) { stringChars[i] = chars[random.Next(chars.Length)]; } var finalString = new String(stringChars); VisitorReg newReg = new VisitorReg(); newReg.VisitorReg_code = finalString; newReg.VisitorReg_email = email; newReg.VisitorReg_date = DateTime.Now.Date; db.VisitorReg.Add(newReg); db.SaveChanges(); SmtpClient smtpClient = new SmtpClient(); smtpClient.Credentials = new NetworkCredential("*****@*****.**", "hb2017cms"); smtpClient.EnableSsl = true; MailMessage message = new MailMessage(); try { MailAddress fromAddress = new MailAddress("*****@*****.**"); smtpClient.Host = "smtp-mail.outlook.com"; smtpClient.Port = 587; message.From = fromAddress; message.To.Add(email); message.Subject = "Your visit code for OSMH"; message.IsBodyHtml = true; message.Body = "Please present this code when you visit us: " + finalString; smtpClient.Send(message); } catch (Exception ex) { result = new { Success = ex.Message }; } return(Json(result, JsonRequestBehavior.DenyGet)); }