Integrate WorkOS AuthKit with .NET (ASP.NET Core). Backend authentication with DI registration, auth endpoints, and appsettings configuration.
Install
npx skillscat add workos/cli/workos-dotnet Install via the SkillsCat registry.
WorkOS AuthKit for .NET (ASP.NET Core)
Step 1: Fetch SDK Documentation (BLOCKING)
STOP. Do not proceed until complete.
WebFetch: https://raw.githubusercontent.com/workos/workos-dotnet/main/README.md
The README is the source of truth for SDK API usage. If this skill conflicts with README, follow README.
Step 2: Pre-Flight Validation
Project Structure
- Confirm a
*.csprojfile exists in the project root - Detect project style:
- Minimal API (modern):
Program.cswithWebApplication.CreateBuilder()— .NET 6+ - Startup pattern (older):
Startup.cswithConfigureServices()/Configure()— .NET 5 and earlier
- Minimal API (modern):
This detection determines WHERE to register WorkOS services and middleware.
Environment Variables
Check appsettings.Development.json for:
WORKOS_API_KEY— starts withsk_WORKOS_CLIENT_ID— starts withclient_
Step 3: Install SDK
dotnet add package WorkOS.netVerify: Check the *.csproj file contains a <PackageReference Include="WorkOS.net" entry.
If dotnet CLI is not available, stop and inform the user to install the .NET SDK.
Step 4: Configure WorkOS Client
Minimal API Pattern (Program.cs)
Add WorkOS configuration to Program.cs:
- Read WorkOS settings from
IConfiguration - Register the WorkOS client in the DI container
- The WorkOS client needs API key for initialization
// In Program.cs, after builder creation:
var workosApiKey = builder.Configuration["WorkOS:ApiKey"];
var workosClientId = builder.Configuration["WorkOS:ClientId"];Startup Pattern (Startup.cs)
Add to ConfigureServices():
- Read WorkOS settings from
IConfiguration - Register services
Choose the pattern that matches the detected project structure.
Step 5: Create Authentication Endpoints
Create auth endpoints following the WorkOS AuthKit pattern. Use minimal API app.MapGet() for minimal API projects, or a Controller for Startup-pattern projects.
Required Endpoints
GET /auth/login — Redirect to WorkOS AuthKit:
- Use the WorkOS SDK to generate an authorization URL
- Include
clientId,redirectUri, andprovider: "authkit"parameters - Redirect the user to the authorization URL
GET /auth/callback — Handle OAuth callback:
- Extract
codefrom query parameters - Exchange authorization code for user profile using the WorkOS SDK
- Store user info in session or cookie
- Redirect to home page
GET /auth/logout — Clear session:
- Clear the authentication session/cookie
- Redirect to home page
Session Management
Use ASP.NET Core's built-in session or cookie authentication:
// Enable session middleware in Program.cs
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession();
// ...
app.UseSession();Step 6: Environment Setup
Configure appsettings.Development.json with WorkOS credentials:
{
"WorkOS": {
"ApiKey": "<WORKOS_API_KEY value>",
"ClientId": "<WORKOS_CLIENT_ID value>",
"RedirectUri": "http://localhost:5000/auth/callback"
}
}Use the actual credential values provided in the environment context.
Important: Do NOT put secrets in appsettings.json (committed to git). Use appsettings.Development.json (gitignored) or dotnet user-secrets.
Step 7: Verification
Run these checks — do not mark complete until all pass:
# 1. Check WorkOS.net is in csproj
grep -i "WorkOS" *.csproj
# 2. Check auth endpoints exist
grep -r "auth/login\|auth/callback\|auth/logout" *.cs
# 3. Build succeeds
dotnet buildIf build fails: Read the error output carefully. Common issues:
- Missing
usingstatements for WorkOS namespaces - Incorrect DI registration order
- Missing session/cookie middleware registration
Error Recovery
"dotnet: command not found"
- .NET SDK is not installed. Inform the user to install from https://dotnet.microsoft.com/download
NuGet restore failures
- Check internet connectivity
- Try
dotnet restoreexplicitly beforedotnet build
"No project file found"
- Ensure you're in the correct directory with a
*.csprojfile
Build errors after integration
- Check that all
usingstatements are correct - Verify DI registration order (services before middleware)
- Ensure
app.UseSession()is called before mapping auth endpoints