public static VerificationResult verifyCode(string userId, String code) { VerificationResult result = new VerificationResult(); var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDataContext())); if (userManager.IsInRole(userId, "Guest")) { result.addError("User is already verified!"); return(result); } UserVerificationLog currentLog = db.UserVerificationLogs.Where(a => a.UserId.Equals(userId) && a.Status.Equals("NOT_CONFIRMED") && a.Code.Equals(code)).FirstOrDefault(); if (currentLog != null) { if (currentLog.ExpiryDate.CompareTo(DateTime.Now) < 0) { result.addError("Code is expired, Request new one please!"); return(result); } currentLog.ConfirmationDate = DateTime.Now; currentLog.Status = "CONFIRMED"; currentLog.LastModificationDate = DateTime.Now; db.Entry(currentLog).State = EntityState.Modified; db.SaveChanges(); assignUserToGuestRole(userId); result.addSuccess("Success"); } else { result.addError("Code is wrong"); } return(result); }
public static VerificationResult generateVerificationLog(string userId, String email) { string code = GenerateCode(); List <UserVerificationLog> logs = db.UserVerificationLogs.ToList(); bool found = logs.Where(a => a.Code.Equals(code)).Count() > 0; int j = 0; int limit = (int)Math.Pow(10, Code_Length); while (!found && j < (limit - 1)) { code = GenerateCode(); found = logs.Where(a => a.Code.Equals(code)).Count() > 0; j++; } UserVerificationLog log = new UserVerificationLog(); log.Code = code; log.CreationDate = DateTime.Now; log.ExpiryDate = DateTime.Now.AddHours(2); log.LastModificationDate = DateTime.Now; log.Status = "NOT_CONFIRMED"; log.UserId = userId; log.Email = email; log.IsEmailSent = sendEmail(log.Code, email); db.UserVerificationLogs.Add(log); VerificationResult result = new VerificationResult(); try { db.SaveChanges(); result.status = "200"; result.message = "Code is sent, it's valid for two hours!"; return(result); } catch (DbEntityValidationException e) { string message1 = e.StackTrace; foreach (var eve in e.EntityValidationErrors) { message1 += eve.Entry.State + "\n"; foreach (var ve in eve.ValidationErrors) { message1 += String.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); message1 += "\n"; } } result.status = "500"; result.message = message1; return(result); } }
public static VerificationResult generateVerificationLog(string userId, String email) { UserVerificationLog log = new UserVerificationLog(); log.Code = GenerateCode(); log.CreationDate = DateTime.Now; log.ExpiryDate = DateTime.Now.AddHours(2); log.LastModificationDate = DateTime.Now; log.Status = "NOT_CONFIRMED"; log.UserId = userId; log.Email = email; log.IsEmailSent = sendEmail(log.Code, email); db.UserVerificationLogs.Add(log); VerificationResult result = new VerificationResult(); try { db.SaveChanges(); result.status = "200"; result.message = "Code is sent, it's valid for two hours!"; return(result); } catch (DbEntityValidationException e) { string message1 = e.StackTrace; foreach (var eve in e.EntityValidationErrors) { message1 += eve.Entry.State + "\n"; foreach (var ve in eve.ValidationErrors) { message1 += String.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); message1 += "\n"; } } result.status = "500"; result.message = message1; return(result); } }