UWP addOnLicense.IsActive always true

Viewed 112

I've tried various techniques but can't work out why this code always results in a valid(active?) license, even if the user hasn't bought the app add-on.

The function initialises when the app starts.

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)
    {
        messagetextblock.Text = "An error occurred while retrieving the license.";
        return;
    }

    // Use members of the appLicense object to access license info...

    // the customer can' t access this feature
    messagetextblock.Text = "customer hasn't bought the addon";

    // 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.

        // Specify the kinds of add-ons to retrieve.
        string[] productKinds = { "Durable", "Consumable", "UnmanagedConsumable" };
        List<String> filterList = new List<string>(productKinds);
        StoreProductQueryResult queryResult = await context.GetAssociatedStoreProductsAsync(filterList);

        if (addOnLicense.IsActive)
        {
            // the customer can access this feature
            messagetextblock.Text = "customer has bought the addon";
        }
    }
    
}
1 Answers

It's hard to tell exactly what could be wrong with your above example. However, I have an app in the market that uses two different types of in app purchases. One to remove ads and one that gets a premium license.

The difference between how you're doing it and I'm doing it is I'm looking for subscription store id in my license skues that are returned with the license.

have you tried this method yet?

        public static void CheckForPremiumStatus()
        {
#if DEBUG
            if (LicenseInformation.ProductLicenses["PremiumStatus"].IsActive)
            {
                IsPremium = RemoveAds = true;
            }
#else
            var subscriptionStoreId = "9PMT47KC5W6C";

            foreach (var addOnLicense in _appLicense.AddOnLicenses)
            {
                StoreLicense license = addOnLicense.Value;
                if (license.SkuStoreId.StartsWith(subscriptionStoreId))
                {
                    if (license.IsActive)
                    {
                        IsPremium = RemoveAds = true;
                        return;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            IsPremium = RemoveAds = false;
#endif
        }

You can see the full example here - feel free to copy and paste it if you want. Just swap out the addon store id's

Related