Home > Software engineering >  How to get the data of the user who is logged in with Google or Facebook
How to get the data of the user who is logged in with Google or Facebook

Time:03-29

is there a function or method in the application that takes parameters such as email addresses, names of people who log in with google or facebook? if there is, how is it.

when I logged in with google, I could access it with the following codes. but I have no idea how to get their information with facebook.

 public class GoogleUserRequest
{
    public const string PROVIDER = "google";

    [JsonProperty("idToken")]
    [Required]
    public string IdToken { get; set; }
}



public async Task<UserApp> AuthenticateGoogleUserAsync(GoogleUserRequest request)
    {
        Payload payload = await ValidateAsync(request.IdToken, new ValidationSettings()
        {
            Audience = new[] {"GOOGLE CLİENT ID"}
        });

        return await ExternalLoginUser(GoogleUserRequest.PROVIDER, payload.Subject, payload.Email, payload.GivenName,
                payload.FamilyName);
    }

with these codes, I can keep factors such as name, email, emal verification via payload variable. If I want to create the same with facebook (Meta), what kind of coding should I do.

CodePudding user response:

is there a function or method in the application that takes parameters such as email addresses, names of people who log in with google or facebook?

You can get the external login information for the current login by using SignInManager<TUser>.GetExternalLoginInfoAsync(String) method:

public class HomeController: Controller
{
    private readonly SignInManager<IdentityUser> _signInManager;

    public ExternalLoginModel(SignInManager<IdentityUser> signInManager)
    {
        _signInManager = signInManager;
    }

    public async Task<UserApp> AuthenticateGoogleUserAsync(GoogleUserRequest request)
    {          
        var info = await _signInManager.GetExternalLoginInfoAsync();
        var name = info.Principal.Identity.Name;
        var email = info.Principal.FindFirst(ClaimTypes.Email).Value;
    }
}
  • Related