HI WELCOME TO KANSIRIS

How to get FirstName + LastName from the user's google account authenticated using Asp.Identity in MVC5 application

Leave a Comment
You can get this inside the ExternalLoginCallback method in AccountController (as you are using out of the box project template) as explained below.
    [AllowAnonymous]
    public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if (loginInfo == null)
        {
            return RedirectToAction("Login");
        }

        if (loginInfo.Login.LoginProvider == "Google")
        {
                var externalIdentity = AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
                var emailClaim = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email);
                var lastNameClaim = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Surname);
                var givenNameClaim = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == ClaimTypes.GivenName);

                var email = emailClaim.Value;
                var firstName = givenNameClaim.Value;
                var lastname = lastNameClaim.Value;
        }

     }

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.