Create new user

DikongPrigm 61 Reputation points
2022-02-03T02:15:04.43+00:00

I do have an ASP.NET Core web api with an Angular frontend and I'm trying to use Facebook Login. Currently, I can successfully login using an angular social login module that I got from angularx-social-login and it returns token and some information about the facebook account. My problem right now is I'm trying to save the facebook credentials into my user store but received an error that I can't add the new facebook user as it doesn't have any password.

public async Task<ActionResult<UserDto>> Facebook([FromBody] FacebookAuthViewModel model)
        {


            var userInfoResponse = await Client.GetStringAsync(
 $"https://graph.facebook.com/v2.8/me?fields=id,email,first_name,last_name,name,gender,locale,birthday,picture&access_token={model.AccessToken}");
            var userInfo = JsonConvert.DeserializeObject<FacebookUserData>(userInfoResponse);


            var user = await _userManager.FindByEmailAsync(userInfo.Email);

            if (user == null)
            {
                var appUser = new AppUser
                {
                    FirstName = userInfo.FirstName,
                    LastName = userInfo.LastName,
                    FacebookId = userInfo.Id,
                    Email = userInfo.Email,
                    UserName = userInfo.Email,
                    PictureUrl = userInfo.Picture.Data.Url
                };             

                await _userManager.CreateAsync(appUser,
                   null);              

            }

            var localUser = await _userManager.FindByNameAsync(userInfo.Email);
            if (localUser == null)
            {
                return BadRequest(new ApiResponse(401, "Failed to create local user account."));
            }

            return new UserDto
            {
                DisplayName = localUser.DisplayName,
                Token = _tokenService.CreateToken(localUser),
                Email = localUser.Email,
            };
        }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,242 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 57,966 Reputation points
    2022-02-03T18:12:10.98+00:00

    while you can override the password policy, you can just use a dummy password.

    what is the point of creating a user account? your server is just trusting that the client verified the facebook account, rather than verifying the facebook token itself.


  2. Zhi Lv - MSFT 32,046 Reputation points Microsoft Vendor
    2022-02-04T09:19:49.903+00:00

    Hi @DikongPrigm ,

    To create a password and sign in using your email that you set during the sign in process with external providers:

    Select the Hello <email alias> link at the top-right corner to navigate to the Manage view. Like this (in this sample, I'm using Microsoft login, after click the link the result as below):

    171250-image.png

    Set a valid password and you can use this to sign in with your email.

    More detail information, see Optionally set password.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments