Configure ASP.NET Identity with JWT and cookie authentication
✓Works with OpenClaudeYou are an ASP.NET Core backend developer. The user wants to configure both JWT and cookie authentication in an ASP.NET Identity setup so requests can be authenticated via either bearer tokens or session cookies.
What to check first
- Run
dotnet --versionto ensure you have .NET 6+ installed - Verify
Microsoft.AspNetCore.IdentityandMicrosoft.AspNetCore.Authentication.JwtBearerare in your.csprojdependencies
Steps
- Install required NuGet packages:
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCoreanddotnet add package Microsoft.AspNetCore.Authentication.JwtBearer - Create or update your
DbContextto inherit fromIdentityDbContext<IdentityUser>and configure the default schema inOnModelCreating - In
Program.cs, add the identity service:builder.Services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<YourDbContext>().AddDefaultTokenProviders() - Configure JWT settings by reading from
appsettings.json— extractJwtSettings:SecretKey,JwtSettings:Issuer, andJwtSettings:Audience - Add authentication schemes in
Program.csusingAddAuthentication()with bothJwtBearerandCookiedefault schemes - For JWT bearer, set
TokenValidationParameterswith the secret key, issuer, and audience from appsettings - For cookies, configure
CookieAuthenticationOptionswithLoginPath,AccessDeniedPath, andSlidingExpiration - Call
app.UseAuthentication()andapp.UseAuthorization()in the middleware pipeline beforeMapControllers()
Code
// appsettings.json
{
"JwtSettings": {
"SecretKey": "your-super-secret-key-at-least-32-characters-long!",
"Issuer": "YourApp",
"Audience": "YourAppUsers",
"ExpirationMinutes": 60
}
}
// Program.cs
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
var builder = WebApplicationBuilder.CreateBuilder(args);
// Add DbContext
builder.Services.AddDbContext<YourDbContext>();
// Add Identity
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<YourDbContext>()
.AddDefaultTokenProviders();
// Read JWT settings
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
var secretKey = Encoding.ASCII.GetBytes(jwtSettings["SecretKey
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related .NET / C# Skills
Other Claude Code skills in the same category — free to download.
ASP.NET Core API
Scaffold ASP.NET Core Web API with controllers and middleware
Entity Framework Core
Set up Entity Framework Core with migrations and queries
Minimal API
Build minimal APIs with endpoint mapping
Blazor
Create Blazor components for interactive web UI
.NET Testing
Write xUnit tests with Moq and FluentAssertions
SignalR
Implement real-time communication with SignalR
.NET Background Services
Create background services with IHostedService
Want a .NET / C# skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.