> For the complete documentation index, see [llms.txt](https://easyauth.papelship.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://easyauth.papelship.com/csharp-document/main-functions/validate-key.md).

# Validate Key

License key validation is the primary mechanism for verifying user entitlement, checking subscription expiry dates, and binding user machines to specific products.

## Authentication Workflow

```mermaid
sequenceDiagram
    participant CSharp as C# Application
    participant Core as Native Security Core
    participant Server as EasyAuth Server
    
    CSharp->>Core: Authenticate(licenseKey, apiKey)
    Core->>Server: Encrypted Auth Request
    Server-->>Core: Encrypted Response (Token, Expiry, Status)
    Core->>Core: Start Watchdog Thread & Heartbeat
    Core-->>CSharp: EasyAuthResult (Success, ExpireDate, ErrorCode)
```

## C# Implementation

{% tabs %}
{% tab title="Standard Validation" %}

```csharp
using System;
using qPapelEasyAuth;

public class LicenseService
{
    private const string ApiKey = "pk_00000066_17fb6e089c7300b122454334e672ec24";

    public static bool TryValidate(string licenseKey, out string message, out string expiry)
    {
        message = string.Empty;
        expiry = string.Empty;

        // Perform validation against server
        var result = qPapelEasyAuth.qPapelEasyAuth.Authenticate(licenseKey, ApiKey);

        if (result.Success)
        {
            expiry = string.IsNullOrEmpty(result.ExpireDate) ? "Lifetime" : result.ExpireDate;
            message = "Authentication successful.";
            return true;
        }

        message = $"{result.Message} (Error Code: {result.ErrorCode})";
        return false;
    }
}
```

{% endtab %}

{% tab title="Detailed Error Handling" %}

```csharp
var result = qPapelEasyAuth.qPapelEasyAuth.Authenticate(licenseKey, apiKey);

if (!result.Success)
{
    switch (result.ErrorCode)
    {
        case 1001: // KEY_NOT_FOUND
            Console.WriteLine("The entered license key does not exist.");
            break;
        case 1002: // KEY_EXPIRED
            Console.WriteLine($"Your subscription expired on {result.ExpireDate}.");
            break;
        case 1003: // HWID_MISMATCH
            Console.WriteLine("This key is registered to another computer. Contact support for a reset.");
            break;
        case 1004: // KEY_BANNED
            Console.WriteLine("This license key has been banned due to policy violation.");
            break;
        case 1005: // MAX_USERS_EXCEEDED
            Console.WriteLine("Concurrent session limit reached for this license.");
            break;
        default:
            Console.WriteLine($"Authentication failed: {result.Message}");
            break;
    }
}
```

{% endtab %}
{% endtabs %}

## Return Value Reference (`EasyAuthResult`)

| Property      | Type     | Description                                                                          |
| ------------- | -------- | ------------------------------------------------------------------------------------ |
| `Success`     | `bool`   | `true` if the license is valid and active; otherwise `false`.                        |
| `ErrorCode`   | `int`    | Numeric error code returned by the authentication server. Returns `0` on success.    |
| `ErrorName`   | `string` | Machine-readable error code identifier (e.g. `KEY_EXPIRED`, `HWID_MISMATCH`).        |
| `Message`     | `string` | Human-readable explanation suitable for UI display.                                  |
| `ExpireDate`  | `string` | Formatted expiration date string (e.g. `2026-12-31 23:59:59` or empty for lifetime). |
| `RawResponse` | `string` | Raw JSON response payload returned by the server.                                    |

{% hint style="info" %}
Upon successful validation, the native core automatically initializes the background watchdog timer to continuously monitor memory integrity and handle automatic heartbeat signals.
{% endhint %}
