1

I am trying to get a steam user avatar, after he logs in with OpenID. But I don't know how to retrieve the database user right after login (to add the avater to the database manually)

I added the new attribute to the Application User:

// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser
{
    public string AvatarPath { get; set; }
}

I created a new Migration and updated the DB. I am trying to alter the data for the user, right after login, but how do I retrieve the current user?!

AccountController:

// 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);
if (result.Succeeded)
{
    //Get Steam User
    if(info.LoginProvider.ToLower() == "steam")
    {
        //I need the DB User

    }

    _logger.LogInformation(5, "User logged in with {Name} provider.", info.LoginProvider);
    return RedirectToLocal(returnUrl);
}

I tried this:

ApplicationUser u = await GetCurrentUserAsync();

u is null

DoubleVoid
  • 777
  • 1
  • 16
  • 46

1 Answers1

3

Found the solution:

var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false);
if (result.Succeeded)
{
    //Get Steam User
    if(info.LoginProvider.ToLower() == "steam")
    {
        var user = await _userManager.FindByLoginAsync("Steam", info.ProviderKey);
        user.AvatarPath = SteamApi.GetSteamAvatar(playerData);
        var ir = await _userManager.UpdateAsync(user);
        if(!ir.Succeeded)
        {
            foreach (var err in ir.Errors)
            {
                Console.WriteLine(err.Code + " - " + err.Description);
            }
        }
    }
}

To fix errors with steam users containing UTF characters, see this: .NET MVC 6 / vNext UserValidator to allow alphanumeric characters Settings AllowedUserNameCharacters to string.empty skips the user name validation.

Only problem, the UserManager does not update the data in memory, redirection to my HomeController still shows old data ... but the database got updated correctly

Community
  • 1
  • 1
DoubleVoid
  • 777
  • 1
  • 16
  • 46