public async Task <IActionResult> Register([FromBody] RegisterViewModel model) { if (ModelState.IsValid) { var user = new FairfieldIdentityUser { UserName = model.UserName, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, DateOfBirth = model.DateOfBirth, Sex = model.Sex, HomePhone = model.HomePhone }; var result = await userManager.CreateAsync(user, model.Password); //var user = new ApplicationUser { UserName = model.UserName, Email = model.Email }; if (result.Succeeded) { var token = await userManager.GenerateEmailConfirmationTokenAsync(user); var confirmationLink = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, token = token }, Request.Scheme); logger.Log(LogLevel.Warning, confirmationLink); if (signInManager.IsSignedIn(User) && User.IsInRole("Admin")) { return(RedirectToAction("ListUsers", "Administration")); } ViewBag.ErrorTitle = "Registration successful"; ViewBag.ErrorMessage = "Before you can Login, please confirm your " + "email, by clicking on the confirmation link we have emailed you"; //return View("Error"); return(Ok(new { status = "Success" })); } else { return(Ok(new { status = "Failure" })); } //foreach (var error in result.Errors) //{ // ModelState.AddModelError(string.Empty, error.Description); //} } return(Ok(new { status = "Failure" })); //return View(model); }
ExternalLoginCallback(string returnUrl = null, string remoteError = null) { returnUrl = returnUrl ?? Url.Content("~/"); LoginViewModel loginViewModel = new LoginViewModel { ReturnUrl = returnUrl, ExternalLogins = (await signInManager.GetExternalAuthenticationSchemesAsync()).ToList() }; if (remoteError != null) { ModelState.AddModelError(string.Empty, $"Error from external provider: {remoteError}"); return(View("Login", loginViewModel)); } var info = await signInManager.GetExternalLoginInfoAsync(); if (info == null) { ModelState.AddModelError(string.Empty, "Error loading external login information."); return(View("Login", loginViewModel)); } var email = info.Principal.FindFirstValue(ClaimTypes.Email); FairfieldIdentityUser user = null; if (email != null) { user = await userManager.FindByEmailAsync(email); if (user != null && !user.EmailConfirmed) { ModelState.AddModelError(string.Empty, "Email not confirmed yet"); return(View("Login", loginViewModel)); } } var signInResult = await signInManager.ExternalLoginSignInAsync( info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true); if (signInResult.Succeeded) { return(LocalRedirect(returnUrl)); } else { if (email != null) { if (user == null) { user = new FairfieldIdentityUser { UserName = info.Principal.FindFirstValue(ClaimTypes.Email), Email = info.Principal.FindFirstValue(ClaimTypes.Email) }; await userManager.CreateAsync(user); var token = await userManager.GenerateEmailConfirmationTokenAsync(user); var confirmationLink = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, token = token }, Request.Scheme); logger.Log(LogLevel.Warning, confirmationLink); ViewBag.ErrorTitle = "Registration successful"; ViewBag.ErrorMessage = "Before you can Login, please confirm your " + "email, by clicking on the confirmation link we have emailed you"; return(View("Error")); } await userManager.AddLoginAsync(user, info); await signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } ViewBag.ErrorTitle = $"Email claim not received from: {info.LoginProvider}"; ViewBag.ErrorMessage = "Please contact support on [email protected]"; return(View("Error")); } }