# License Keys Setup Guide

This document explains how to configure license keys for MediatR 13.x and AutoMapper 15.x.

## Overview

ColaFlow API uses commercial versions of:
- **MediatR 13.1.0** - Requires a license key
- **AutoMapper 15.1.0** - Requires a license key (if used)

## Configuration Methods

### Option 1: User Secrets (Recommended for Development)

This is the **most secure** method for local development:

```bash
cd colaflow-api/src/ColaFlow.API

# Set MediatR license key
dotnet user-secrets set "MediatR:LicenseKey" "your-actual-mediatr-license-key-here"

# Set AutoMapper license key (if you use AutoMapper)
dotnet user-secrets set "AutoMapper:LicenseKey" "your-actual-automapper-license-key-here"
```

**Advantages:**
- Secrets are stored outside the project directory
- Never accidentally committed to Git
- Scoped per-user, per-project

### Option 2: appsettings.Development.json (Quick Setup)

For quick local setup, you can directly edit the configuration file:

1. Open `colaflow-api/src/ColaFlow.API/appsettings.Development.json`

2. Replace the placeholder values:

```json
{
  "MediatR": {
    "LicenseKey": "your-actual-mediatr-license-key-here"
  },
  "AutoMapper": {
    "LicenseKey": "your-actual-automapper-license-key-here"
  }
}
```

**Warning:** Be careful not to commit actual license keys to Git!

### Option 3: Environment Variables (Production)

For production environments, use environment variables:

```bash
# Windows (PowerShell)
$env:MediatR__LicenseKey = "your-actual-mediatr-license-key-here"
$env:AutoMapper__LicenseKey = "your-actual-automapper-license-key-here"

# Linux/Mac (Bash)
export MediatR__LicenseKey="your-actual-mediatr-license-key-here"
export AutoMapper__LicenseKey="your-actual-automapper-license-key-here"
```

**Note:** Use double underscores `__` to represent nested configuration keys.

### Option 4: Azure Key Vault (Production - Most Secure)

For production on Azure, store license keys in Azure Key Vault:

```csharp
// In Program.cs
builder.Configuration.AddAzureKeyVault(
    new Uri($"https://{keyVaultName}.vault.azure.net/"),
    new DefaultAzureCredential()
);
```

Then create secrets in Azure Key Vault:
- `MediatR--LicenseKey`
- `AutoMapper--LicenseKey`

## Verification

After setting up the license keys, verify the configuration:

### 1. Build the project

```bash
cd colaflow-api
dotnet clean
dotnet restore
dotnet build
```

**Expected:** No license warnings in the build output.

### 2. Run the API

```bash
cd src/ColaFlow.API
dotnet run
```

**Expected:** API starts without license warnings in the console.

### 3. Check the logs

Look for startup logs - there should be **no warnings** about missing or invalid license keys.

## Troubleshooting

### Warning: "No license key configured for MediatR"

**Solution:** Ensure the license key is correctly configured using one of the methods above.

### Warning: "Invalid license key for MediatR"

**Possible causes:**
1. The license key is incorrect or expired
2. The license key contains extra whitespace (trim it)
3. The license key is for a different version

**Solution:** Verify the license key with the vendor.

### Configuration not loading

**Check:**
1. User secrets are set for the correct project
2. appsettings.Development.json is valid JSON
3. Environment variables use double underscores `__` for nested keys
4. The application is running in the expected environment (Development/Production)

## Getting License Keys

### MediatR License
- Purchase from: https://www.mediator.dev/licensing
- Contact: license@mediator.dev

### AutoMapper License
- Purchase from: https://www.automapper.org/
- Contact: support@automapper.org

## Security Best Practices

1. **Never commit license keys to Git**
   - Use `.gitignore` to exclude sensitive files
   - Use User Secrets for local development
   - Use environment variables or Key Vault for production

2. **Rotate keys regularly**
   - Update license keys when they expire
   - Remove old keys from all environments

3. **Limit access**
   - Only share keys with authorized team members
   - Use separate keys for development and production if possible

4. **Monitor usage**
   - Track which applications use which license keys
   - Audit key usage regularly

## Configuration Priority

.NET configuration uses the following priority (highest to lowest):

1. Command-line arguments
2. Environment variables
3. User secrets (Development only)
4. appsettings.{Environment}.json
5. appsettings.json

Later sources override earlier ones.

## Support

If you encounter issues with license key configuration:

1. Check this guide for troubleshooting steps
2. Verify your license key validity with the vendor
3. Contact the development team for assistance

---

**Last Updated:** 2025-11-03
**ColaFlow Version:** 1.0.0
**MediatR Version:** 13.1.0
**AutoMapper Version:** 15.1.0
