Identity Framework Web API(.Net 8) how to control the Token Expire time

I have been using Identity Framework packages in my ASP.Net Core Web API Project(.Net8). I have the following setup:

On the Program.cs:

//The AddIdentityApiEndpoints add the authentication itself so i don't need to declare it. The problem is I don't have any way to control the Token

builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("GeneralAccess", policy =>
           policy.RequireAssertion(context =>
            context.User.IsInRole("Admin") ||
            context.User.IsInRole("User")));
}); 

//Setup Database Context
builder.Services.AddDbContext<DemoSecurityDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("SecurityConnection")));


//This will add the API endpoints for the Identity DB
builder.Services
    .AddIdentityApiEndpoints<DemoUser>(options => options.SignIn.RequireConfirmedAccount = true)
    //.AddIdentity<DemoUser, DemoIdentityRoles>()
    .AddRoles<DemoIdentityRoles>()
    .AddEntityFrameworkStores<DemoSecurityDbContext>();

app.MapGroup("/account").MapIdentityApi<DemoUser>();

I have been digging around the MapIdentityApi and the SignManager to understand how can I change the Token expiry date, at the moment is set to 3600 but I can’t find an easy way to overwrite this value.
I can’t see where the token is generated in the code and 3600 is too much
{
“tokenType”: “Bearer”,
“accessToken”: ““,
“expiresIn”: 3600,
“refreshToken”: “

}

I have been looking across all the visible code.
I have been searching on the web but no one has raised this question.
Ideally I would like a simple answer where I can use the built in code from the Indentity Framework API ./Account/Login endpoint.

Here is the code from from the AddIdentityApiEndpoints it doesn’t look I have any control on the token

public static IdentityBuilder AddIdentityApiEndpoints<TUser>(this IServiceCollection services, Action<IdentityOptions> configure)
        where TUser : class, new()
    {
        ArgumentNullException.ThrowIfNull(services);
        ArgumentNullException.ThrowIfNull(configure);

        services
            .AddAuthentication(IdentityConstants.BearerAndApplicationScheme)
            .AddScheme<AuthenticationSchemeOptions, CompositeIdentityHandler>(IdentityConstants.BearerAndApplicationScheme, null, compositeOptions =>
            {
                compositeOptions.ForwardDefault = IdentityConstants.BearerScheme;
                compositeOptions.ForwardAuthenticate = IdentityConstants.BearerAndApplicationScheme;
            })
            .AddBearerToken(IdentityConstants.BearerScheme)
            .AddIdentityCookies();

        return services.AddIdentityCore<TUser>(configure)
            .AddApiEndpoints();
    }

To change the token expiry duration in ASP.NET Core Identity JWT tokens, you can modify the TokenValidationParameters when configuring JWT authentication. In your Program.cs, where you configure authentication using JWT bearer, you can adjust the TokenValidationParameters as follows:

    builder.Services.AddAuthentication(options =>
{
    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
{
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuerSigningKey = true,
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"])),
        ValidateLifetime = true,
        ClockSkew = TimeSpan.Zero, // Remove the clock skew
        ValidateIssuer = false,
        ValidateAudience = false,
    };
});

setting ClockSkew to TimeSpan.Zero, you effectively disable the clock skew, meaning the token will expire strictly according to the exp claim value in the token payload.
Now, to set the token expiration time explicitly, you can do it while generating the token. If you’re using SignInManager to generate tokens, you can specify the expiration time there. Here’s how you can do it:

var signInResult = await _signInManager.PasswordSignInAsync(user, password, rememberMe, lockoutOnFailure: true);
if (signInResult.Succeeded)
{
    // Generate token
    var tokenDescriptor = new SecurityTokenDescriptor
    {
        Subject = new ClaimsIdentity(new[]
        {
            new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
            // Add other claims as needed
        }),
        Expires = DateTime.UtcNow.AddMinutes(60), // Set the expiration time here
        SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"])), SecurityAlgorithms.HmacSha256Signature)
    };

    var tokenHandler = new JwtSecurityTokenHandler();
    var token = tokenHandler.CreateToken(tokenDescriptor);

    return Ok(new
    {
        tokenType = "Bearer",
        accessToken = tokenHandler.WriteToken(token),
        expiresIn = 3600, // This value will be overwritten by the expiration time set in tokenDescriptor
        refreshToken = ""
    });
}

1

Add the below to your Program.cs

builder.Services.AddAuthentication().AddBearerToken(IdentityConstants.BearerScheme,
o =>
{
    o.BearerTokenExpiration = TimeSpan.FromSeconds(60);
}

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật