public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new Membership { UserName = model.UserName, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); MvcCaptcha.ResetCaptcha("RegistrationCaptcha"); return View("RegistrationSuccessful"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }
protected async Task<LogInStatus> ValidateUserAsync(Membership user, string password) { if (user == null) { return LogInStatus.EmailNotExist; } else if (await UserManager.IsLockedOutAsync(user.Id)) { return LogInStatus.LockedOut; } else if (!await UserManager.IsEmailConfirmedAsync(user.Id)) { return LogInStatus.RequiresConfirmation; } else if (!await UserManager.CheckPasswordAsync(user, password)) { if (await UserManager.GetLockoutEnabledAsync(user.Id)) { await UserManager.AccessFailedAsync(user.Id); } return LogInStatus.IncorrectPassword; } else { return LogInStatus.Success; } }
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return RedirectToAction("Index", "Manage"); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return View("ExternalLoginFailure"); } var user = new Membership { UserName = model.UserName, EmailConfirmed = true, LockoutEnabled = false }; var result = await UserManager.CreateExternalAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return View(model); }