Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the Google/Facebook first name/surname in ASP.NET Identity?

I'm building an application using ASP.NET MVC and Identity and I have Google logins working, but I'd like to display the user's name (from Google/Facebook), so that rather than the website showing as "Hi [email protected]" it displays as "Hi Fred". I'm aware of how to customise the ApplicationUser, it's just the interaction with the providers I'm not too sure about.

Can anyone help?

like image 377
Tim Almond Avatar asked Oct 18 '25 10:10

Tim Almond


1 Answers

On ExternalLoginCallback method you can get all the details.

  //code omitted for clarity
  public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if (loginInfo == null)
        {
            return RedirectToAction("Login");
        }

        var identity = AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
        var emailClaim = identity.Result.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email);
        var lastNameClaim = identity.Result.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Surname);
        var givenNameClaim = identity.Result.Claims.FirstOrDefault(c => c.Type == ClaimTypes.GivenName);

        var email = emailClaim.Value;
        var firstName = givenNameClaim.Value;
        var lastname = lastNameClaim.Value;
     }
like image 108
Kishore Sahasranaman Avatar answered Oct 21 '25 01:10

Kishore Sahasranaman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!