Other account information with facebook authentication

4

I am implementing facebook authentication and wanted to, when entering the application with the account data, also save full name, photo, among other data.

Searching, I got the following code:

  facebookOptions.Events = new Microsoft.AspNetCore.Authentication.OAuth.OAuthEvents
  {
       OnCreatingTicket = context => {
       string surName = context.User.Value<string>("last_name");
       context.Identity.AddClaim(new System.Security.Claims.Claim(ClaimTypes.Surname, surName));

       return Task.FromResult(0); 
       }
  };

The user's surname appears in the variable surName but is not saved anywhere.

If someone can help me understand what is Identity.AddClaim and how to save the data that comes in User.Value<T> . Thanks

    
asked by anonymous 20.11.2017 / 23:56

1 answer

4

I was able to solve what I needed.

  

You do not need the code you were using in the question.

Just that, with AccountController in action ExternalLoginCallback you pass the values contained in Claims to the model:

    [HttpGet]
    [AllowAnonymous]
    public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
    {
        if (remoteError != null)
        {
            ErrorMessage = $"Error from external provider: {remoteError}";
            return RedirectToAction(nameof(Login));
        }
        var info = await _signInManager.GetExternalLoginInfoAsync();
        if (info == null)
        {
            return RedirectToAction(nameof(Login));
        }

        // 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, isPersistent: false, bypassTwoFactor: true);
        if (result.Succeeded)
        {
            _logger.LogInformation("User logged in with {Name} provider.", info.LoginProvider);
            return RedirectToLocal(returnUrl);
        }
        if (result.IsLockedOut)
        {
            return RedirectToAction(nameof(Lockout));
        }
        else
        {
            // If the user does not have an account, then ask the user to create an account.
            ViewData["ReturnUrl"] = returnUrl;
            ViewData["LoginProvider"] = info.LoginProvider;
            var email = info.Principal.FindFirstValue(ClaimTypes.Email);

            //apenas esse código para pegar o valor do Name que veio do provedor
            var name = info.Principal.FindFirstValue(ClaimTypes.Name);


            return View("ExternalLogin", new ExternalLoginViewModel { Email = email, Nome = name  }); //valor da variável repassado para o model

        }
    }
    
21.11.2017 / 01:13