© 2026 Hedgehog Software, LLC
public static IServiceCollection AddTokenAuthentication(this IServiceCollection services, string key) { var keybytes = Encoding.ASCII.GetBytes(key); services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(x => x.TokenValidationParameters = new TokenValidationParameters { IssuerSigningKey = new SymmetricSecurityKey(keybytes), ValidateIssuer = true, ValidateAudience = true, ValidIssuer = "localhost", ValidAudience = "localhost" }); return services; }
services.AddAuthentication
System.InvalidOperationException: 'The service collection cannot be modified because it is read-only.'