Exemple #1
0
        public async Task <IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
        {
            if (remoteError != null)
            {
                return(Render(ExternalLoginStatus.Error));
            }
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                return(Render(ExternalLoginStatus.Invalid));
            }

            // Sign in the user with this external login provider if the user already has a login.
            var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false);

            var email = info.Principal.FindFirstValue(ClaimTypes.Email);

            if (result.Succeeded)
            {
                var            user  = _userManager.FindByEmailAsync(email).Result;
                IList <string> roles = _userManager.GetRolesAsync(user).Result;

                if (_accountHelper.IsAccountActive(email))
                {
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    return(RedirectToAction("Unauthorized"));
                }
            }
            if (result.RequiresTwoFactor)
            {
                return(Render(ExternalLoginStatus.TwoFactor));
            }
            if (result.IsLockedOut)
            {
                return(Render(ExternalLoginStatus.Lockout));
            }
            else
            {
                ViewData["ReturnUrl"]     = returnUrl;
                ViewData["LoginProvider"] = info.LoginProvider;
                info.Principal.FindFirstValue(ClaimTypes.Email);

                string roleName = _accountHelper.GetRoleForEmail(email);

                if (string.IsNullOrEmpty(roleName))
                {
                    return(RedirectToAction("NotRegistered"));
                }

                var user = new QRecruitmentUser {
                    UserName = email, Email = email, CreatedDate = DateTime.Now
                };
                var createUserTaskResult = _userManager.CreateAsync(user).Result;


                IdentityResult roleCreationResult = null;

                if (!_roleManager.RoleExistsAsync(roleName).Result)
                {
                    roleCreationResult = _roleManager.CreateAsync(new QRecruitmentRole(roleName)).Result;
                }

                if (createUserTaskResult.Succeeded)
                {
                    var createLoginTaskResult   = _userManager.AddLoginAsync(user, info).Result;
                    var addUserToRoleTaskResult = _userManager.AddToRoleAsync(user, roleName).Result;
                    if (createLoginTaskResult.Succeeded)
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(RedirectToAction("Index", "Home"));
                    }
                }

                if (createUserTaskResult.Errors.First().Code == "DuplicateUserName")
                {
                    return(RedirectToAction("DuplicateUserError"));
                }


                return(RedirectToAction("UserCreationError"));
            }
            //else
            //{
            //    // If the user does not have an account, then ask the user to create an account.
            //    // ViewData["ReturnUrl"] = returnUrl;
            //    // ViewData["LoginProvider"] = info.LoginProvider;
            //    // var email = info.Principal.FindFirstValue(ClaimTypes.Email);
            //    // return RedirectToAction("Index", "Home", new ExternalLoginCreateAccountViewModel { Email = email });
            //    return Render(ExternalLoginStatus.CreateAccount);
            //}
        }