應用情境與驗收
為隔離的 C++ 工作站啟用時,在原裝置產生申請檔案,移至連網電腦取得簽章回應,再匯入原裝置。裝置不符或簽章授權到期時應拒絕。離線用戶端無法立即得知伺服器端撤銷;執行結果受簽章有效期與更新規則約束。
1. 選擇允許離線的策略
為隔離網路中的 C++ 軟體匯出裝置綁定的啟用請求,在可連上網際網路的電腦核准後,將簽章回應帶回原電腦匯入。此檔案交換流程需要允許離線啟用的裝置綁定授權;浮動授權仍需建立線上工作階段。
固定裝置在最後一次成功連線後的上線期限到期時歸還工作空間額度,裝置綁定仍保留。浮動裝置在最後一個工作階段釋放或租約到期時歸還額度。
- 在控制台「產品」複製產品 ID,點擊同一產品的「簽章公鑰」,將每一組金鑰 ID、公鑰填入初始化程式碼,取代 YOUR_PRODUCT_ID、YOUR_KEY_ID 和 YOUR_PUBLIC_KEY。快速接入也可直接複製已填好的程式碼。無需設定檔;SDK 預設連線至 https://www.licentivo.com。
2. 安裝原生 SDK
先安裝 CMake 3.21 或更新版本。Windows x64 使用 Visual Studio 2022 C++ Build Tools 與相符的 MSVC SDK 套件,並指定 -G "Visual Studio 17 2022" -A x64;Linux x64 使用 C11/C++17 編譯器。將 SDK_PREFIX 替換為安裝程式輸出的 CMake 前綴路徑。 將範例原始碼儲存為 offline.cpp,並將下方 CMakeLists.txt 儲存到同一目錄。先在可連線的電腦完成安裝與編譯,再將執行檔及其執行階段相依項目複製到離線電腦。
請安裝與編譯器、架構相符的共用版 libcurl 開發套件。Windows MSVC x64 使用者需將 CURL_DIR 替換為包含 CURLConfig.cmake 的目錄,在本機執行時將 curl 的 bin 目錄加入 PATH,發佈應用程式時一併提供所需 DLL;Linux 使用者請安裝發行版提供的共用版 libcurl 開發套件。
& ([scriptblock]::Create((Invoke-WebRequest 'https://www.licentivo.com/assets/sdk/releases/v0.11.9/install.ps1').Content)) -Language cpp -Toolchain msvccurl --fail --silent --show-error --location --proto '=https' --proto-redir '=https' 'https://www.licentivo.com/assets/sdk/releases/v0.11.9/install.sh' | bash -s -- cppcmake_minimum_required(VERSION 3.18)
project(offline_activation LANGUAGES C CXX)
find_package(Licentivo 0.11.9 REQUIRED)
add_executable(offline offline.cpp)
target_link_libraries(offline PRIVATE Licentivo::CPP)
set_target_properties(offline PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON)cmake -S . -B build -G "Visual Studio 17 2022" -A x64 -DCMAKE_PREFIX_PATH="SDK_PREFIX" -DCURL_DIR="CURL_DIR"
if ($LASTEXITCODE -ne 0) { throw 'CMake configure failed. Check the SDK prefix and matching libcurl development package.' }
cmake --build build --config Release --target offline
if ($LASTEXITCODE -ne 0) { throw 'Build failed.' }set -e
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="SDK_PREFIX"
cmake --build build --target offline3. 用 SDK 匯出申請與匯入回應
將範例存為 offline.cpp,加入已設定 SDK 的 CMake 專案。在離線電腦執行 offline request YOUR_LICENSE_KEY,產生 activation.request.json;過程不啟動連線用戶端。
把申請帶到連線電腦,在廠商控制台或已關聯電子郵件的客戶入口上傳申請,下載核准回應並命名為 activation.response.json。將申請與回應一同帶回原電腦,執行 offline import YOUR_LICENSE_KEY。
#include "licentivo.hpp"
#include <fstream>
#include <iostream>
#include <iterator>
#include <stdexcept>
#include <string>
// BEGIN LICENTIVO PRODUCT
// Copy Product ID and all key ID/public key pairs from Console > Products > Signing public keys.
static const std::string PRODUCT_ID = "YOUR_PRODUCT_ID";
static const std::string TRUSTED_KEYS_JSON = R"({"YOUR_KEY_ID":"YOUR_PUBLIC_KEY"})";
// END LICENTIVO PRODUCT
std::string readText(const char* path) {
std::ifstream file(path);
if (!file) throw std::runtime_error("Cannot read file");
return {std::istreambuf_iterator<char>(file), {}};
}
int main(int argc, char** argv) {
try {
// Product settings are in code; the key is entered by the customer.
if (argc < 3) throw std::runtime_error("Usage: offline request|import LICENSE_KEY");
auto client = licentivo::Client::ForProduct(PRODUCT_ID, TRUSTED_KEYS_JSON, argv[2]);
if (std::string(argv[1]) == "request") {
std::ofstream file("activation.request.json");
file << client.OfflineRequest();
if (!file) throw std::runtime_error("Cannot save request");
std::cout << "Created activation.request.json\n";
} else if (std::string(argv[1]) == "import") {
client.ImportOffline(readText("activation.request.json"),
readText("activation.response.json"));
client.RunFeature("export", [] {
std::ofstream file("licensed-report.txt");
file << "Licensed offline export\n";
if (!file) throw std::runtime_error("Cannot save report");
});
std::cout << "Created licensed-report.txt\n";
} else {
throw std::runtime_error("Unknown operation");
}
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}
}./build/Release/offline.exe request YOUR_LICENSE_KEY./build/Release/offline.exe import YOUR_LICENSE_KEY./build/offline request YOUR_LICENSE_KEY./build/offline import YOUR_LICENSE_KEY4. 確認啟用結果
- 確認匯入成功且產生 licensed-report.txt。
- 以相同快取位置及儲存的裝置憑證重新啟動;仍有效的離線租約應允許已授權功能。
- 在另一台一般電腦匯入相同回應,裝置驗證應拒絕。
- 檢查授權到期時間與離線時間。匯入檔案不會延長原授權,也不能超出簽章允許範圍。
使用 C 介面
C 應用程式使用 ln_init_product,傳入產品 ID、受信任的公開金鑰與客戶授權碼,建立尚未啟動的用戶端。透過 ln_offline_request 產生請求,再以 ln_import_offline 驗證並匯入請求與回應。傳回的字串以 ln_free_string 釋放,用戶端則以 ln_destroy 釋放。
首次完全離線啟用時,請勿呼叫 ln_start 或 ln_open,因為它們會嘗試連線啟動。使用上述請求與匯入流程,由 SDK 驗證簽章回應,請勿在業務程式碼中自行解析簽章。
常見失敗與使用限制
檢查產品 ID、每組金鑰 ID 和公鑰、客戶輸入的授權碼。公鑰屬於產品;客戶授權與本機憑證分別管理。開發測試授權碼及廠商管理 API Key 不隨客戶安裝包發送。
回應無效可能是申請、產品、裝置、簽章公鑰或應用程式版本不符。必須帶回產生申請的電腦,不可修改 JSON 內容或停用驗證。
申請到期須重新產生並核准。離線租約到期須依策略續期或再次交換核准檔案。完全離線時無法進行線上計次的預留與確認。
遠端解除綁定或撤銷須等舊裝置下次連線檢查才會得知。永久離線的裝置無法立即得知伺服器變更,設定允許離線時間時應考慮此限制。
設定與下一步
範例實測
測試環境: Windows x64, Visual Studio 2022 17.14.32, MSVC 19.44, Windows SDK 10.0.26100.0, native SDK 0.11.8, shared libcurl 8.22.0, MySQL 8.4.13
使用獨立本機 API、MySQL 與 SDK 0.11.8 測試;測試設定僅調整 API 位址與私有快取位置。本輪未驗證公開套件庫下載、Electron、Linux 或 macOS。
cmake -S . -B build -G "Visual Studio 17 2022" -A x64 -DCMAKE_PREFIX_PATH="SDK_PREFIX" -DCURL_DIR="CURL_DIR"
cmake --build build --config Release --target offline
./build/Release/offline.exe request YOUR_LICENSE_KEY
./build/Release/offline.exe import YOUR_LICENSE_KEYVisual Studio 2022 Release build: PASS
Offline request and approved import: PASS, protected report created
Import with a tampered signature: rejected, no file written
Original approved response after the rejected import: PASS
Expired signed grant after a real wait under a 5-second test policy: rejected, no file written