google signin - ASP.NET MVC : Owin external login returns redirect inside the login popup - Stack Overflow

I have an external login that I am utilizing Microsoft.Owin to authenticate the user.It is working cor

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:

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");
    }
}

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:

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");
    }
}
Share Improve this question edited Jan 29 at 17:35 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 29 at 16:36 EricEric 2581 gold badge3 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745289979a4620796.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信