取得應用程式和附加元件的授權資訊Get license info for apps and add-ons

本文示範如何使用 Windows.Services.Store 命名空間中的 StoreContext 類別的方法,來取得目前前應用程式及其附加元件的授權資訊。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. 例如,您可以使用這項資訊來判斷 app 或其附加元件的授權是否有效,或者它們是否為試用版授權。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.

注意

Windows.Services.Store 命名空間在 Windows 10 (版本 1607) 中引進,只適用於目標為 Visual Studio 中 Windows 10 Anniversary Edition (10.0;組建 14393) 或更新版本的專案。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. 如果您的 app 目標為較早版本的 Windows 10,您必須使用 Windows.ApplicationModel.Store 命名空間,而不是 Windows.Services.Store 命名空間。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:

  • 適用於目標為 Windows 10 Anniversary Edition (10.0;組建 14393) 或更新版本的通用 Windows 平台 (UWP) App 的 Visual Studio 專案。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:

  • 程式碼會在 Page 的內容中執行,其中包含名為 workingProgressRingProgressRing 和名為 textBlockTextBlockThe code runs in the context of a Page that contains a ProgressRing named workingProgressRing and a TextBlock named textBlock. 這些物件可個別用來表示發生非同步作業,以及顯示輸出訊息。These objects are used to indicate that an asynchronous operation is occurring and to display output messages, respectively.
  • 程式碼檔案含有適用於 Windows.Services.Store 命名空間的 using 陳述式。The code file has a using statement for the Windows.Services.Store namespace.
  • App 是單一使用者 app,僅會在啟動 app 的使用者內容中執行。The app is a single-user app that runs only in the context of the user that launched the app. 如需詳細資訊,請參閱 App 內購買和試用版For more information, see In-app purchases and trials.

注意

如果您的傳統型應用程式使用傳統型橋接器,您可能需要新增此範例中未顯示的額外程式碼來設定 StoreContext 物件。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. 如需詳細資訊,請參閱在使用傳統型橋接器的傳統型應用程式中使用 StoreContext 類別For more information, see Using the StoreContext class in a desktop application that uses the Desktop Bridge.

程式碼範例Code example

若要取得目前 App 的授權資訊,請使用 GetAppLicenseAsync 方法。To get license info for the current app, use the GetAppLicenseAsync method. 這是一個非同步方法,會傳回 StoreAppLicense 物件來提供應用程式的授權資訊,包括指出使用者目前是否具備使用應用程式的有效授權 (IsActive) 及該授權是否是試用版授權 (IsTrial) 的屬性。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).

若要存取使用者具有使用權利的目前應用程式的持久附加元件授權,請使用 StoreAppLicense物件的 AddOnLicenses 屬性。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. 此屬性會傳回代表附加元件授權的集合 StoreLicense 物件。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.