Search code examples
uwpwindows-store-appswindows-10windows-storexbox-one

StoreContext User property is always null in single-user UWP application


I'm developing an application for UWP (Universal Windows Platform) with availability on Windows 10 and Xbox One devices. The app is a single user application. I'm trying to determine at runtime a unique identifier for the active signed-in user -- just an ID without any personally identifiable information. The documentation for the StoreContext class suggests that I should be able to determine this by examining the User property on the default StoreContext object; specifically, the NonRoamableId property seems appropriate. However, in every context I've run the app in (debug, deployed, Windows, Xbox), the StoreContext's User property has always been null. Is this expected? I'm unable to find any other way to determine this information.

Of note: I'm aware of the UserWatcher class and its methods for querying users on the system. However, I would like to avoid using this for the following reasons:

  1. The properties exposed to the app by using these query methods are insufficient for determining which User is the one associated with the current StoreContext, meaning that such a solution would be error-prone.
  2. Querying for user information this way results in a permissions pop-up, which makes sense if the app needs to read personally identifiable information, but since I'm just looking for a programmatic unique identifier, this doesn't seem like it should be necessary.

I'm developing against the Windows 10 Creators' Update SDK (10.0.15063).

Edit 11/16/2017

This looks like it may have been a UWP bug that is now fixed. With no code changes, I now see a non-null User object attached to the LaunchActivatedEventArgs on app launch. Accepted the answer accordingly.


Solution

  • A related question here.

    Active user signed in with MS account can be obtained from the OnLaunched/Activated event arguments.

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
       Windows.System.User active_user = e.User;
    }
    

    This requires adding User Account Information capability in package manifest.