Jack O'Reilly
Jack O'Reilly

Reputation: 442

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.

Upvotes: 3

Views: 1134

Answers (3)

I know I'm late to the answer here, but others may find this question too. The User property on the StoreContext is not designed to be a way to retrieve a user. In fact, it shouldn't be possible from a UWP to get much details about the user that is currently signed into the store.

The User property is there to allow developers of multi-user applications that use the "StoreContext.GetForUser(...) API to create a StoreContext. When this method is used, then the User property will be the user provided when it was created.

StoreContext.GetDefault() will ALWAYS have a null User property.

The multi-user model is designed for Xbox One where there can be multiple users signed in at the same time.

Upvotes: 2

Bells
Bells

Reputation: 1493

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.

Upvotes: 4

Nico Zhu
Nico Zhu

Reputation: 32785

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.

It's more recommended to use Windows.Services.Store namespace to get user ID in Windows 10 Creators' Update. However this namespace does not provided class that you can use to simulate during testing.

So you are supposed to publish your app to store and download the app to your development device for testing so that you can get a valid StoreContext.

For single-user application:

In a single-user app (that is, an app that runs only in the context of the user that launched the app), use the static GetDefault method to get a StoreContext object that you can use to access Windows Store-related data for the user.

Windows.Services.Store.StoreContext context = StoreContext.GetDefault();

Upvotes: 1

Related Questions