AbsoluteSith
AbsoluteSith

Reputation: 1967

How to get the username of the current logged in user in UWP?

I tried checking out this sample, where we can get all users and their info, But when I execute IReadOnlyList<User> users = await User.FindAllAsync(); I don't get any values.

And moreover I need to get the current logged in user and not all users, PS : I have requested the required capabilities.

Upvotes: 1

Views: 876

Answers (1)

Peter Torr
Peter Torr

Reputation: 12019

For most devices, FindAllAsync method returns the user that is currently logged-in and using the app; it doesn't return all the accounts on the machine. Xbox supports two or more people playing the same game at once, so it can return more than one user at a time.

This works fine for me; all you need is the userAccountInformation capability:

var res = await User.FindAllAsync(UserType.LocalUser);
foreach (var r in res)
{
  var props = await r.GetPropertiesAsync(new string[] { KnownUserProperties.DisplayName });
  foreach (var p in props)
  {
    Debug.Write(p.Key + ": " + p.Value);
  }
}

Upvotes: 2

Related Questions