Estoy tratando de implementar JWT en una aplicacion, pero tengo el siguiente problema : Establesco el tiempo de expiracion de dicho token en 2 minutos entonces me logeo y obtengo el token , espero que pasen los 2 minutos y hago una peticion a mi aplicacion enviando el token pero la aplicacion de igual manera me deja acceder a los actions no niega el acceso, como puedo solucionar esto ??
Mi codigo :
StartUp.cs
public class Startup
{
private IConfigurationBuilder builder;
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
this.builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
EngineData.DefaultConnection = Configuration["ConnectionStrings:ConexionDb"];
EngineData.UrlBase = Configuration["Site:UrlBase"];
EngineData.JwtKey = Configuration["Jwt:Key"];
EngineData.JwtIssuer = Configuration["Jwt:Issuer"];
EngineData.JwtAudience = Configuration["Jwt:Audience"];
//EntityFramework
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ShineContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("ConexionDb")));
//JasonWebToken JWt
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
};
});
services.AddMvc();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc();
}
}
Metodo que genera mi token
private string GenerateJSONWebToken(User userInfo)
{
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(EngineData.JwtKey));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
var KClaims = new[] {
new Claim(JwtRegisteredClaimNames.Sub, userInfo.Username),
new Claim(JwtRegisteredClaimNames.GivenName, userInfo.Name),
new Claim(JwtRegisteredClaimNames.Email, userInfo.Email),
new Claim("ExpiracionToken", DateTime.UtcNow.AddMinutes(2).ToString()),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
};
var token = new JwtSecurityToken(EngineData.JwtKey,
EngineData.JwtIssuer,
claims:KClaims,
expires: DateTime.UtcNow.AddMinutes(2),
signingCredentials: credentials);
return new JwtSecurityTokenHandler().WriteToken(token);
}