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 ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(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);
        }
Exemplo n.º 2
0
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl = null)
        {
            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 custEmail = FindEmail(model.Email);
                var custUserName = FindUserName(model.ExtUsername);
                var user = new ApplicationUser
                {
                    UserName = model.ExtUsername,
                    Email = model.Email,
                    FirstName = model.ExtFirstName,
                    LastName = model.ExtLastName,
                    Country = model.ExtCountry,
                    BirthDate = model.ExtBirthDate,
                    JoinDate = DateTime.Now,
                    LastLoginDate = DateTime.Now,
                    EmailLinkDate = DateTime.Now
                };

                if (custEmail == null && custUserName == null)
                {
                    var result = await UserManager.CreateAsync(user);
                    if (result.Succeeded)
                    {
                        UserManager.AddToRole(user.Id, "Candidate");
                        result = await UserManager.AddLoginAsync(user.Id, info.Login);
                        if (result.Succeeded)
                        {
                            codeType = "EmailConfirmation";
                            await SendEmail("ConfirmEmail", "Account", user, model.Email, "WelcomeEmail", "Confirm your account");
                            return RedirectToAction("ConfirmationEmailSent", "Account");
                        }
                    }
                    AddErrors(result);
                }
                else
                {
                    if (custEmail != null)
                    {
                        ModelState.AddModelError("", "Email is already registered.");
                    }
                    if (custUserName != null)
                    {
                        ModelState.AddModelError("", "Username " + model.ExtUsername.ToLower() + " is already taken.");
                    }
                }
            }
            ViewBag.ReturnUrl = returnUrl;
            return View("ExternalLoginConfirmation");
        }