public int CreateUser(User user) { //Check account does not already exist if (ModelState.IsValid) { try { //Encode user password user.Password = Convert.ToBase64String(new MD5CryptoServiceProvider().ComputeHash(new System.Text.UTF8Encoding().GetBytes(user.Password ?? ""))); db.User.Add(user); db.SaveChanges(); } catch (Exception e) { return -1; //Account already exists or other database error } try { //Send welcome mail to user Mail mail = new Mail(); mail.Welcome(user.Id).Send(); } catch (Exception e) { } return user.Id; //Success } return -2; //Data model validation failed }
/// Send a mail to inform a user with a matching route wish to contact /// Result 0=success, 1=error (sending mail failed) \n /// Call example: http://commute.apphb.com/api/apiroute?routeId=1&matchingRouteId=2 public int GetSendMatchingRouteMail(int routeId, int matchingRouteId) { try { //Send welcome mail to user Mail mail = new Mail(); mail.Contact(routeId, matchingRouteId).Send(); return 0; //Success } catch ( Exception e ) { return 1; //Something wrong happened } }
public ActionResult ResetPassword(User postUser) { //Retrieve current user User user; try { user = (from u in db.User where u.Account == postUser.Account select u).FirstOrDefault(); } catch (Exception ex) { return RedirectToAction("Error", "Home", new Error("User", "ResetPassword", ex.Message + ex.InnerException.Message)); } //Account not found if (user == null) ModelState.AddModelError("Account", Resources.Error_unknown_account); //Control mail match the one registered for this account else if (user.EmailAddress != postUser.EmailAddress) ModelState.AddModelError("EmailAddress", Resources.Error_wrong_mail); //Password is mandatory we removed from ModelState ModelState.Remove("Password"); //Generate a new password - password is mandatory in the model string password = Membership.GeneratePassword(12, 1); if (ModelState.IsValid) { //Update user password try { user.Password = Convert.ToBase64String(new MD5CryptoServiceProvider().ComputeHash(new UTF8Encoding().GetBytes(password))); db.SaveChanges(); } catch (Exception ex) { return RedirectToAction("Error", "Home", new Error("User", "ResetPassword", ex.Message + ex.InnerException.Message)); } //Send new reset password mail user.Password = password; //we need to send to user the password not the hash we saved to database Mail mail = new Mail(); mail.Password(user).Send(); return RedirectToAction("Login"); //return RedirectToAction("Password", "Mail", user); } else return View(user); //Cannot send mail }
public ActionResult Register(User user) { //Check account is free int count = db.User.Count(u => u.Account == user.Account); if ( count > 0 && user.Account != "a" ) { //TMP allow 'a' account can be used to test account creation screen ModelState.AddModelError("Account", Resources.Error_duplicate_account); return View(); } if (ModelState.IsValid) { if (user.Account != "a") //TMP 'a' account is not re-created { //Computer password hash user.Password = Convert.ToBase64String(new MD5CryptoServiceProvider().ComputeHash(new UTF8Encoding().GetBytes(user.Password ?? ""))); db.User.Add(user); db.SaveChanges(); } else user.Id = 1; //TMP need to set user Id for 'a' account //Authenticate user FormsAuthentication.SetAuthCookie(user.Account, true); //true=Persistent cookie Session["userId"] = user.Id; //TMP //Go to /User/WelcomeRegistered screen //return RedirectToAction("WelcomeRegistered", new { mailJustSent = 1 }); //Send welcome mail to user Mail mail = new Mail(); mail.Welcome(user.Id).Send(); //Go to /User/WelcomeRegistered screen return RedirectToAction("WelcomeRegistered", new { mailJustSent = 1 }); } return View(); }
//Send contact mail public string MailContact(int fromRouteId, int toRouteId) { RouteCompare routeCompare = new RouteCompare(fromRouteId, toRouteId); Mail mail = new Mail(); mail.Contact(fromRouteId, toRouteId).Send(); return "OK"; }