3

Asp.Net core application, I have integrated third parties login authentication, "Twitter,Facebook" etc, however the problem is: "My User Identity context has First and Last Name", which is required but when i try to authenticate through external providers, it wont have an options to add these fields. How can I add these fields with external login providers, so my database will have first and last name along with its external providers credential details.

Thanks,

1

1 Answer 1

3

Well, the answer until some code is provided is "it depends", but I will try. Yesterday I did a similar research, so it's kinda fresh knowledge. As a prerequisite I created a fresh Razor pages project and went through these tutorials for different providers.

Firstly, different providers will give you different data. For instance, without any additional configuration you'll get first and last names only from Facebook and Google. In order to get the name from Twitter you'll need to manually add the claim

.AddTwitter(options =>
            {
                options.ConsumerKey = Configuration["Authentication:Twitter:ConsumerKey"];
                options.ConsumerSecret = Configuration["Authentication:Twitter:ConsumerSecret"];
                options.RetrieveUserDetails = true;
                options.ClaimActions.MapJsonKey(ClaimTypes.GivenName, "name");
            })

or somehow handle the CreatingTicket event. Problems won't stop here. In Twitter they don't distinguish the first and last names, so it's just Name. And icing on the cake: in Twitter and Google you can erase your name so you won't get any data in that case.

Secondly, keeping the data. I think one can simply follow the approach suggested in article already mentioned and save claims and/or extend the IdentityUser class, which I believe is your case. I put the user update call where they saved the claims, in the OnPostConfirmationAsync:

if (result.Succeeded) {   
  // Save claims. This will populate AspNetUserClaims table
  await _userManager.AddClaimsAsync(user, 
  new []
  {
    info.Principal.FindFirst(ClaimTypes.GivenName),
    info.Principal.FindFirst(ClaimTypes.Surname)
  });

  // AND/OR save user. This will populate the AspNetUsers table
  user.FirstName = info.Principal.FindFirst(ClaimTypes.GivenName).Value;
  user.LastName = info.Principal.FindFirst(ClaimTypes.Surname).Value;
  await _userManager.UpdateAsync(user);
  ...
}

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.