Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Google login in MVC Core

$
0
0

Hi everybody,

Please let me know why "identity.External" cookie got lost or removed after doing register by Google external authentication.  It seems the cookie dies so soon.

Info is null after user registration .

 public IActionResult ExternalLogin(string provider = "Google", string returnUrl = null)
        {
            // Request a redirect to the external login provider.
            var redirectUrl = Url.Action(nameof(ExternalLoginCallback), "ApiAccount", new { ReturnUrl = returnUrl });
            var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
            return Challenge(properties, provider);
        }


        // GET: /Account/ExternalLoginCallback
        [HttpGet]
        [AllowAnonymous]
        [Route("ExternalLoginCallback")]
        public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
        {
            if (remoteError != null)
            {
                return Redirect(returnUrl);
            }
            var info = await _signInManager.GetExternalLoginInfoAsync();
            if (info == null)
                return Redirect(returnUrl + ";error=true");

            // 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, false);
            if (result.Succeeded)
            {
                 
                _logger.LogInformation(5, "User logged in with {Name} provider.", info.LoginProvider);
                return Redirect(returnUrl + ";email=");
            }

            // If the user does not have an account, then ask the user to create an account.

            var email = info.Principal.FindFirstValue(ClaimTypes.Email);
            return Redirect(returnUrl + ";email=" + email); // send email
        }

        //
        // POST: /Account/ExternalLoginConfirmation
        [HttpGet]
        [AllowAnonymous]
        [Route("ExternalLoginConfirmation")]
        //[ValidateAntiForgeryToken]
        public async Task<TServiceResult<TokenModel>> ExternalLoginConfirmation(string email, string phoneNumber)
        {
            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await _signInManager.GetExternalLoginInfoAsync();
                if (info == null)
                    return new TServiceResult<TokenModel>(null, "Not Exist Google Info!", false);

                ApplicationUser user = _userManager.Users.FirstOrDefault(us => us.UserName == phoneNumber);
                IdentityResult res = null;

                if (user == null)
                {
                    var us = new UserModel
                    {
                        UserName = phoneNumber,
                        Email = email,
                        PhoneNumber = phoneNumber,
                        FName = info.Principal.HasClaim(c => c.Type == ClaimTypes.GivenName) ? info.Principal.FindFirstValue(ClaimTypes.GivenName) : phoneNumber,
                        LName = info.Principal.HasClaim(c => c.Type == ClaimTypes.Surname) ? info.Principal.FindFirstValue(ClaimTypes.Surname) : ""
                    };

                    user = await _repo.RegisterUserGoogle(us);
                }

                res = await _userManager.AddLoginAsync(user, info);
                if (res.Succeeded)
                {
                    await _signInManager.SignInAsync(user, false);
                    _logger.LogInformation(6, "User created an account using {Name} provider.", info.LoginProvider);
                    return await GotoRefreshToken(user.UserName,100);
                }

                return new TServiceResult<TokenModel>(null, "Error in Create Google Login!", false);
            }

            return new TServiceResult<TokenModel>(null, "Error Validation", false);
        }

        [HttpGet]
        [AllowAnonymous]
        [Route("GetExternalLoginToken")]
        //[ValidateAntiForgeryToken]
        public async Task<TServiceResult<TokenModel>> GetExternalLoginToken()
        {
            var info = await _signInManager.GetExternalLoginInfoAsync();
            if (info == null)
                return new TServiceResult<TokenModel>(null, "Not Exist Google Info!", false);

            var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, false);
            if (result.Succeeded)
            {
                if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email))
                {
                    var email = info.Principal.FindFirstValue(ClaimTypes.Email);
                    var user = _userManager.Users.FirstOrDefault(u => u.Email == email);
                    if (user != null)
                    {
                        return await GotoRefreshToken(user.UserName, 100);
                    }
                }
            }
            return new TServiceResult<TokenModel>(null, "Not Exist Google Info!", false);
        }


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>