Search code examples
asp.net-mvcgoogle-signinowin

ASP.NET MVC : Owin external login returns redirect inside the login popup


I have an external login that I am utilizing Microsoft.Owin to authenticate the user.

It is working correctly, where the user can sign in and register using their Google Login.

My problem is, the redirect is redirecting inside the actual login popup, and not the main screen of the user. I want to refresh the users screen on a successful login which I am trying to accomplish via a redirect.

The screenshot shown here might be able to explain in more detail what is happening:

enter image description here

Here is the code that is hit after the external login:

public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

    if (loginInfo == null)
    {
        return RedirectToAction("Login");
    }

    // Sign in the user with this external login provider if the user already has a login
    var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);

    switch (result)
    {
        case SignInStatus.Success:
            return RedirectToLocal(returnUrl);

        case SignInStatus.LockedOut:
            return View("Lockout");

        case SignInStatus.RequiresVerification:
            return RedirectToAction("SendCode", new { ReturnUrl = returnUrl });

        case SignInStatus.Failure:
        default:
            // If the user does not have an account, then prompt the user to create an account
            var existingUser =  UserManager.FindByEmail(loginInfo.Email);

            if (existingUser != null)
            {  
                // Link the external login to the existing user
                var addLoginResult = await UserManager.AddLoginAsync(exisitingUser.Id, loginInfo.Login);

                if (addLoginResult.Succeeded)
                {
                    await SignInManager.SignInAsync(exisitingUser, isPersistent: false, rememberBrowser: false);
                    return RedirectToLocal(returnUrl);
                }
            }

        return Redirect("LoginFailure");
    }
}

Solution

  • I finally figured this out right after posting the question

    replacing the

     return RedirectToLocal(returnUrl);
    

    with this line of code

    return Content("<script> if(window.opener) {window.opener.location.reload(); window.close();} else{window.location = '" + Url.Action("Index", "Home") + "';} </script>", "text/html");
    

    so passing back javascript to check for the popup, which will close it, and also refresh the page.