Reputation: 442
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:
User
is the one associated with the current StoreContext
, meaning that such a solution would be error-prone.I'm developing against the Windows 10 Creators' Update SDK (10.0.15063).
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
Reputation: 66
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
Reputation: 1493
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
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