Get license info for apps and add-ons
This article demonstrates how to use methods of the StoreContext class in the Windows.Services.Store namespace to get license info for the current app and its add-ons. For example, you can use this info to determine if the licenses for the app or its add-ons are active, or if they are trial licenses.
Note
The Windows.Services.Store namespace was introduced in Windows 10, version 1607, and it can only be used in projects that target Windows 10 Anniversary Edition (10.0; Build 14393) or a later release in Visual Studio. If your app targets an earlier version of Windows 10, you must use the Windows.ApplicationModel.Store namespace instead of the Windows.Services.Store namespace. For more information, see this article.
Prerequisites
This example has the following prerequisites:
- A Visual Studio project for a Universal Windows Platform (UWP) app that targets Windows 10 Anniversary Edition (10.0; Build 14393) or a later release.
- You have created an app submission in Partner Center and this app is published in the Store. You can optionally configure the app so it is not discoverable in the Store while you test it. For more information, see our testing guidance.
- If you want get license info for an add-on for the app, you must also create the add-on in Partner Center.
The code in this example assumes:
- The code runs in the context of a Page that contains a ProgressRing named
workingProgressRing
and a TextBlock namedtextBlock
. These objects are used to indicate that an asynchronous operation is occurring and to display output messages, respectively. - The code file has a using statement for the Windows.Services.Store namespace.
- The app is a single-user app that runs only in the context of the user that launched the app. For more information, see In-app purchases and trials.
Note
If you have a desktop application that uses the Desktop Bridge, you may need to add additional code not shown in this example to configure the StoreContext object. For more information, see Using the StoreContext class in a desktop application that uses the Desktop Bridge.
Code example
To get license info for the current app, use the GetAppLicenseAsync method. This is an asynchronous method that returns a StoreAppLicense object that provides license info for the app, including properties that indicate whether the user currently has a valid license to use the app (IsActive) and whether the license is for a trial version (IsTrial).
To access the licenses for durable add-ons of the current app for which the user has an entitlement to use, use the AddOnLicenses property of the StoreAppLicense object. This property returns a collection of StoreLicense objects that represent the add-on licenses.
private StoreContext context = null;
public async void GetLicenseInfo()
{
if (context == null)
{
context = StoreContext.GetDefault();
// If your app is a desktop app that uses the Desktop Bridge, you
// may need additional code to configure the StoreContext object.
// For more info, see https://aka.ms/storecontext-for-desktop.
}
workingProgressRing.IsActive = true;
StoreAppLicense appLicense = await context.GetAppLicenseAsync();
workingProgressRing.IsActive = false;
if (appLicense == null)
{
textBlock.Text = "An error occurred while retrieving the license.";
return;
}
// Use members of the appLicense object to access license info...
// Access the valid licenses for durable add-ons for this app.
foreach (KeyValuePair<string, StoreLicense> item in appLicense.AddOnLicenses)
{
StoreLicense addOnLicense = item.Value;
// Use members of the addOnLicense object to access license info
// for the add-on.
}
}
For a complete sample application, see the Store sample.