> 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/auto-login.md).

# Auto Login

The Auto Login feature allows applications to store encrypted authentication credentials locally using Windows DPAPI and hardware-bound encryption keys. On subsequent launches, the application can authenticate seamlessly without prompting the user.

## Overview

```mermaid
graph TD
    A[Application Launch] --> B{Cached Credentials?}
    B -- Yes --> C[qPapelEasyAuth.AutoLogin]
    B -- No --> D[Prompt User for Key]
    C -- Valid --> E[Launch Main Application]
    C -- Expired/Invalid --> D
    D --> F[qPapelEasyAuth.Authenticate]
    F -- Success & Save --> G[SaveKeyToDisk]
    G --> E
```

## C# Implementation

{% tabs %}
{% tab title="Automatic Authentication Flow" %}

```csharp
using System;
using System.IO;
using qPapelEasyAuth;

public class LoginManager
{
    private const string ApiKey = "pk_00000066_17fb6e089c7300b122454334e672ec24";
    private static readonly string KeyFilePath = Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
        "YourApp",
        "license.dat"
    );

    public static bool PerformLogin()
    {
        // 1. Try reading saved license key
        if (File.Exists(KeyFilePath))
        {
            string savedKey = File.ReadAllText(KeyFilePath).Trim();
            if (!string.IsNullOrEmpty(savedKey))
            {
                Console.WriteLine("[*] Attempting auto-login with saved key...");
                var autoResult = qPapelEasyAuth.qPapelEasyAuth.Authenticate(savedKey, ApiKey);
                if (autoResult.Success)
                {
                    Console.WriteLine("[+] Auto-login successful.");
                    return true;
                }
                
                Console.WriteLine($"[-] Saved key invalid: {autoResult.Message}");
            }
        }

        // 2. Prompt for manual entry if auto-login fails
        Console.Write("Enter License Key: ");
        string keyInput = Console.ReadLine()?.Trim() ?? "";

        var manualResult = qPapelEasyAuth.qPapelEasyAuth.Authenticate(keyInput, ApiKey);
        if (manualResult.Success)
        {
            // Save key for future launches
            Directory.CreateDirectory(Path.GetDirectoryName(KeyFilePath)!);
            File.WriteAllText(KeyFilePath, keyInput);
            Console.WriteLine("[+] License key saved.");
            return true;
        }

        Console.WriteLine($"[-] Authentication failed: {manualResult.Message}");
        return false;
    }
}
```

{% endtab %}

{% tab title="Clear Saved Credentials" %}

```csharp
public static void Logout()
{
    if (File.Exists(KeyFilePath))
    {
        File.Delete(KeyFilePath);
        Console.WriteLine("[+] Saved credentials cleared.");
    }
}
```

{% endtab %}
{% endtabs %}

## Security Considerations

* **Hardware Binding**: Stored keys are tied to the local machine's HWID on the server. If a saved file is copied to another device, the server rejects the login request with `HWID_MISMATCH`.
* **Session Revocation**: If a license is banned or revoked from the administrator panel, subsequent auto-login attempts will fail immediately.

{% hint style="info" %}
Always validate the result of `Authenticate()` even when using cached keys, as subscription periods expire dynamically.
{% endhint %}
