---
title: "sdk/dotnet"
description: ".NET 8 Server-SDK für netzwerklose JWT-Prüfung, ASP.NETCore-Anfrage-Authentifizierung und Webhook-Signaturvalidierung."
locale: "de"
---

> Documentation Index
> Fetch the locale documentation index at: https://xid.dev/de/llms.txt
> Use this file to discover all available pages before exploring further.

# sdk/dotnet

## Zustand

Implementiert und lokal verifiziert. Die echteIdP-Round-Trip-Verifizierung (JWKS-Abruf, Token-Signierung/Prüfung gegeneine Live-XID-Instanz) wurde noch nicht durchgeführt und muss vor demProduktionseinsatz abgeschlossen werden.

## Installieren

.NET 8 als Zielplattform erforderlich.

```xml
<PackageReference Include="Xid" Version="0.1.0" />
```

## ASP.NET Core-Einrichtung (empfohlen)

```csharp
// Program.cs
using Xid;

builder.Services.AddXid(options =>
{
options.Issuer   = "https://xid.dev";
options.Audience = "your-client-id"; // optional
});
```

## Anfrage authentifizieren

```csharp
// Controller / Minimal API
public class MyController(XidClient xid) : ControllerBase
{
[HttpGet("/me")]
public async Task<IActionResult> GetMe()
{
    var auth = await xid.AuthenticateRequestAsync(
        authorizationHeader: Request.Headers.Authorization,
        cookies: Request.Cookies.ToDictionary(c => c.Key, c => c.Value));

    if (!auth.Authenticated)
        return Unauthorized(auth.Reason);

    return Ok(new { sub = auth.Claims!.Sub, email = auth.Claims.Email });
}
}
```

## Token direkt prüfen

```csharp
using Xid;

var client = new XidClient(new XidOptions { Issuer = "https://xid.dev" });

try
{
var claims = await client.VerifyTokenAsync("eyJ...");
Console.WriteLine($"sub={claims.Sub} email={claims.Email}");
}
catch (TokenVerificationException ex)
{
Console.WriteLine($"Invalid token: {ex.Message}");
}
```

## Webhook prüfen

```csharp
app.MapPost("/webhooks/xid", async (HttpRequest req, XidClient xid) =>
{
using var ms = new MemoryStream();
await req.Body.CopyToAsync(ms);
var body = ms.ToArray();

var headers = new Dictionary<string, string>
{
    ["svix-id"]        = req.Headers["svix-id"].ToString(),
    ["svix-timestamp"] = req.Headers["svix-timestamp"].ToString(),
    ["svix-signature"] = req.Headers["svix-signature"].ToString(),
};

var webhookSecret = Environment.GetEnvironmentVariable("XID_WEBHOOK_SECRET")
    ?? throw new InvalidOperationException("XID_WEBHOOK_SECRET is required");

try
{
    var webhook = xid.VerifyWebhook(body, headers, secret: webhookSecret);
    return Results.Ok();
}
catch (WebhookVerificationException ex)
{
    return Results.BadRequest(ex.Message);
}
});
```

## XidOptions

| Eigenschaft | Standard | Beschreibung |
| --- | --- | --- |
| `Issuer` | erforderlich | XID-Issuer-URL |
| `Audience` | `null` | Erwarteter aud-Claim; null überspringt die Validierung |
| `JwksTtl` | 1 Stunde | JWKS-In-Memory-Cache-TTL |
| `SessionCookieName` | `__session` | Fallback-Cookie-Name für die Token-Extraktion |
| `ClockSkew` | 5 Minuten | JWT-exp/nbf-Taktversatztoleranz |
| `WebhookToleranceWindow` | 5 Minuten | Webhook-Replay-Präventionsfenster |

## XidClient-API

| Methode | Beschreibung |
| --- | --- |
| `VerifyTokenAsync(token, ct)` | JWT-String prüfen; wirft bei Fehler `TokenVerificationException`. |
| `AuthenticateRequestAsync(authHeader, cookies, ct)` | Token extrahieren und prüfen; gibt `AuthStatus` zurück; wirft keineException. |
| `VerifyWebhook(payload, headers, secret)` | Webhook-Signatur validieren; löst bei Fehler`WebhookVerificationException` aus. Synchron. |

## Plattformhinweise

- Verwendet `Microsoft.IdentityModel.Tokens` und`System.IdentityModel.Tokens.Jwt` 8.x. ES256 ist primär; RS256 undPS256 werden unterstützt.
- `AddXid()` registriert `XidClient` als Singleton und verdrahtet`IHttpClientFactory` für den JWKS-Abruf.
- Ausnahmehierarchie: `XidException` -&gt; `JwksException`,`TokenVerificationException`, `WebhookVerificationException`.

Source: https://xid.dev/de/sdks/dotnet/index.mdx
