在 Web.co/nfig 中設定授權金鑰

This article was translated from English: Does it need improvement?
Translated
View the article in English

此問題已於 IronPDF 2024.3.3 版本中解決

Exception: Unhandled exception. IronSoftware.Exceptions.LicensingException: IronPDF must be licensed for development.

針對較舊的 IronPDF 版本,特別是 2023.4.42024.3.3 之間的版本,存在已知授權問題:

  • ASP.NET 專案
  • .NET Framework 版本 >= 4.6.2

儲存於 Web.co/nfig 檔案中的金鑰,產品將不會讀取並使用。

解決方案

為解決此問題,建議在程式碼中使用 ConfigurationManagerWeb.co/nfig 檔案中擷取授權金鑰,然後將其套用至 License.LicenseKey 屬性。

範例:

<configuration>
  ...
  <appSettings>
    <add key="IronPdf.LicenseKey" value="IRONPDF-MYLICENSE-KEY-1EF01"/>
  </appSettings>
  ...
</configuration>
<configuration>
  ...
  <appSettings>
    <add key="IronPdf.LicenseKey" value="IRONPDF-MYLICENSE-KEY-1EF01"/>
  </appSettings>
  ...
</configuration>
XML

透過上述提供的 XML 檔案,我們可以使用 ConfigurationManager 擷取授權金鑰值,並將其傳遞給 IronPdf.License.LicenseKey 屬性。

using System.Co/nfiguration;

class Program
{
    static void Main()
    {
        // Retrieve the license key from the Web.co/nfig file
        string licenseKey = ConfigurationManager.AppSettings["IronPdf.LicenseKey"];

        // Apply the license key to the IronPdf library
        IronPdf.License.LicenseKey = licenseKey;

        // Additional processing can continue here
    }
}
using System.Co/nfiguration;

class Program
{
    static void Main()
    {
        // Retrieve the license key from the Web.co/nfig file
        string licenseKey = ConfigurationManager.AppSettings["IronPdf.LicenseKey"];

        // Apply the license key to the IronPdf library
        IronPdf.License.LicenseKey = licenseKey;

        // Additional processing can continue here
    }
}
Imports System.Configuration

Class Program
    Shared Sub Main()
        ' Retrieve the license key from the Web.config file
        Dim licenseKey As String = ConfigurationManager.AppSettings("IronPdf.LicenseKey")

        ' Apply the license key to the IronPdf library
        IronPdf.License.LicenseKey = licenseKey

        ' Additional processing can continue here
    End Sub
End Class
$vbLabelText   $csharpLabel
Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 19,014,616 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronPdf
執行範例 觀看您的 HTML 轉為 PDF。