public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl = null) { if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { return(View("ExternalLoginFailure")); } var user = new CardCraftUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation(6, "User created an account using {Name} provider.", info.LoginProvider); return(RedirectToLocal(returnUrl)); } } AddErrors(result); } ViewData["ReturnUrl"] = returnUrl; return(View(model)); }
public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new CardCraftUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713 // Send an email with this link //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); //await _emailSender.SendEmailAsync(model.Email, "Confirm your account", // $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>"); await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation(3, "User created a new account with password."); return(RedirectToLocal(returnUrl)); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> Login(LoginViewModel model) { string apiStatus = "successful_login"; string apiMessage = "Successful logged into CardCraft"; CardCraftUser data = null; //Add service code to login in and authenticate against database // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, set lockoutOnFailure: true var result = await SignManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure : false); if (result.Succeeded) { data = await UserManager.FindByEmailAsync(model.Email); Logger.LogInformation(1, APILog(endPoint: "Login", statusCode: "200", apiStatus: apiStatus, apiMessage: apiMessage, data: data)); return(SuccessfulAPIResult(apiStatus, apiMessage, data)); } if (result.RequiresTwoFactor) { apiStatus = "user_requires_2FA"; apiMessage = "Please complete sign up process by checking link in email: " + model.Email; return(SuccessfulAPIResult(apiStatus, apiMessage)); //return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); } if (result.IsLockedOut) { apiStatus = "user_account_locked"; apiMessage = "Your user account is locked please check " + model.Email + "for details."; Logger.LogWarning(2, "User account locked out."); return(SuccessfulAPIResult(apiStatus, apiMessage)); } else { apiStatus = "invalid_login_attempt"; apiMessage = "Error logging you in. Please check email address or password."; ModelState.AddModelError(string.Empty, "Invalid login attempt."); return(ErrorAPIResult(apiStatus, apiMessage)); } //apiStatus = "invalid_login_attempt"; //apiMessage = "There was an issue logging you in. Please try again later or email [email protected]"; //ModelState.AddModelError(string.Empty, "Invalid login attempt."); //return ErrorAPIResult(apiStatus, apiMessage, model); }
public async Task <IActionResult> SignUp(RegisterViewModel model) { string apiStatus = "successful_signup"; string apiMessage = "Successful sign up with CardCraft"; CardCraftUser data = null; var user = new CardCraftUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713 // Send an email with this link //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); //await _emailSender.SendEmailAsync(model.Email, "Confirm your account", // $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>"); data = await UserManager.FindByEmailAsync(model.Email); Logger.LogInformation(3, "User created a new account with password."); return(SuccessfulAPIResult(apiStatus, apiMessage, data)); } else { return(IdentityResultLogicError(result)); } //} //else //{ // apiStatus = "invalid_signup_attempt"; // apiMessage = "There was an issue signing you up. Please try again later or email [email protected]"; // ModelState.AddModelError(string.Empty, "Invalid signup attempt."); // return ErrorAPIResult(apiStatus, apiMessage, model); //} }