I am currently writing my first web API from scratch. I’ve modified plenty of other APIs over the years, but this is the first time I’ve had to build one (besides tinkering on a tutorial). I’m using ASP.Net Core 8.
Following the excellent tutorial at https://www.youtube.com/watch?v=8J3nuUegtL4, I quickly had authentication set up. Exciting!
However, this method always gives me an expiresIn
value of 3600. Is there a way to change this value? I see lots on the web about setting a JWT expiration, but that seems to require me to rip out a lot of what I did in the tutorial and try again.
Here’s code from my Program.cs:
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();
builder.Logging.AddLog4Net();
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
options.OperationFilter<SecurityRequirementsOperationFilter>();
});
builder.Services.AddDbContext<DataContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddAuthorization();
builder.Services.AddIdentityApiEndpoints<IdentityUser>()
.AddEntityFrameworkStores<DataContext>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment() || app.Environment.IsProduction())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapIdentityApi<IdentityUser>();
// We enforce HTTPS on WAF. Enforcing it here as well causes a redirect loop.
//app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
Thanks!