public ActionResult Login(LoginTrainerViewModel model, string returnUrl) { if (ModelState.IsValid) { var sTrainer = ServiceFactory.GetTrainerService(); var newTrainer = new Trainer() { EmailAddress = model.EmailAddress, Name = model.EmailAddress, Password = model.Password }; var trainer = sTrainer.LoginTrainer(model.EmailAddress, model.Password); if (trainer != null) { this.signInTrainer(trainer, model.RememberMe); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("", "Invalid username or password."); } } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult Create(Trainer training) { if (ModelState.IsValid) { this._sTraining.RegisterNewTrainer(training); return RedirectToAction("Index"); } return View(training); }
private void signInTrainer(Trainer trainer, bool rememberMe) { var cookie = FormsAuthentication.GetAuthCookie(trainer.EmailAddress, rememberMe); var ticket = FormsAuthentication.Decrypt(cookie.Value); var newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, trainer.ToJson(1), ticket.CookiePath); var encTicket = FormsAuthentication.Encrypt(newTicket); // Use existing cookie. Could create new one but would have to copy settings over... cookie.Value = encTicket; Response.Cookies.Add(cookie); }
public Trainer RegisterNewTrainer(string emailAddress, string displayName, string password) { if (string.IsNullOrWhiteSpace(emailAddress)) { throw new ArgumentNullException("Trainer must have a valid email address."); } if (displayName == null) { throw new ArgumentNullException("Trainer must have a valid Display Name"); } if (string.IsNullOrWhiteSpace(password) || password.Length < MIN_PASSWORD_LENGTH) { throw new ArgumentException(string.Format("Password must be at least {0} characters.", MIN_PASSWORD_LENGTH)); } // Check if Training name already exists. var trainer = _repTraining.GetByEmailAddress(emailAddress); if(trainer != null) { throw new DuplicateRecordException(String.Format("Cannot register Trainer. The email address '{0}' has already been used.", trainer.Name)); } // Create a new trainer trainer = new Trainer(); trainer.EmailAddress = emailAddress; trainer.Name = displayName; // Create Salt and hash the password. trainer.PasswordSalt = CryptoUtils.CreateSalt(PASSWORD_SALT_LENGTH); trainer.Password = CryptoUtils.CreatePasswordHash(password, trainer.PasswordSalt); // Add a new Training instance. this._repTraining.AddNew(trainer); // Persist changes to database. this._repTraining.SaveChanges(); return trainer; }
public ActionResult Edit(int id, Trainer training) { if (ModelState.IsValid) { //this._sTraining.AddMeAsANewTrainer(training); return RedirectToAction("Index"); } return View(); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { WebSecurity.CreateUserAndAccount(model.EmailAddress, model.Password, new { DisplayName = model.Name }); Response.Redirect("~/account/login"); var newTrainer = new Trainer() { EmailAddress = model.EmailAddress, Name = model.Name, }; this._sTraining.RegisterNewTrainer(newTrainer); var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInAsync(user, isPersistent: false); return RedirectToAction("Index", "Home"); } else { AddErrors(result); } } // If we got this far, something failed, redisplay form return View(model); }
private async Task SignInAsync(Trainer trainer, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await UserManager.CreateIdentityAsync(trainer, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); }