Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC External login - How to skip association form

I want to skip external login register - association form enter image description here
I am using google external login with MVC5, If you login with google account, it will show you above screen after entering google credentials first time. I just want to skip this screen.

Above view get return from

        [AllowAnonymous]
        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 user = await UserManager.FindAsync(loginInfo.Login);
            if (user != null)
            {
                await SignInAsync(user, isPersistent: false);
                return RedirectToLocal(returnUrl);
            }
            else
            {
                // If the user does not have an account, then prompt the user to create an account
                ViewBag.ReturnUrl = returnUrl;
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;

                return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });
            }
        }



return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });

Above code return ExternalLoginConfirmation view and shows above screen. on submit above form it will submitted to below action

public async Task ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)

To skip above register screen, I need to call ExternalLoginConfirmation from ExternalLoginCallback, so How can I do that.

like image 574
vikrantx Avatar asked Mar 15 '23 22:03

vikrantx


1 Answers

Well, didn't take too long to put this together. I haven't tested it, but if it doesn't work as-is, should be really close. I mostly just copied the code from ExternalLoginConfirmation to ExternalLoginCallback:

        // 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, RememberMe = false });
            case SignInStatus.Failure:
            default:

                // Added this section to automatically create account if we have an email address
                if (!string.IsNullOrEmpty(loginInfo.Email))
                {
                    var user = new ApplicationUser { UserName = loginInfo.Email, Email = loginInfo.Email };
                    var createUserResult = await UserManager.CreateAsync(user);
                    if (createUserResult.Succeeded)
                    {
                        var addLoginResult = await UserManager.AddLoginAsync(user.Id, loginInfo.Login);
                        if(addLoginResult.Succeeded)
                        {
                            await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                            return RedirectToLocal(returnUrl);
                        }
                    }
                }

                // If the user does not have an account, then prompt the user to create an account
                ViewBag.ReturnUrl = returnUrl;
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });
        }
like image 153
eselk Avatar answered Mar 17 '23 13:03

eselk