简体   繁体   中英

Asp core web-api session is empty after return ok()

In my project in the auth controller, I create OTP code and return to the user and if a user sends another request for code its check for the session to see if the user gets it before or not but after the new request I check HttpContext.Session.Keys and it's empty

it's only working on postman not any browser

My startup class

services.AddMvcCore(config =>
{
    config.EnableEndpointRouting = false;
    config.ReturnHttpNotAcceptable = true;
    config.Filters.Add(typeof(RequireHttpsAttribute));
    var policy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .Build();
    config.Filters.Add(new AuthorizeFilter(policy));
})
 .AddApiExplorer()
 .AddFormatterMappings()
 .AddDataAnnotations()
 .AddCors(opt =>
    opt.AddPolicy("CorsPolicy", builder =>
    builder.WithOrigins("http://localhost:4200").AllowAnyMethod().AllowAnyHeader().AllowCredentials())
 ).AddNewtonsoftJson(opt =>
    {
        opt.SerializerSettings.ReferenceLoopHandling =
        Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    });
services.AddResponseCaching();
services.AddHsts(opt =>
{
    opt.MaxAge = TimeSpan.FromMinutes(5);
});

And

seeder.SeedUsers();
app.UseRouting();
app.UseStaticFiles(new StaticFileOptions()
{
    RequestPath = new PathString("/wwwroot")
});
app.UseSession();

In another project it's working perfectly I don't know whats wrong

i assume U are using angualr in clicent side

so

In production mode client and the web api on the same site so u have no problem with the session but in development mode your angular is hosted on localhost:4200 which reset the session on each call to a service.

u can host the angular and the web api on the same host on development mode but it means I have to do ng build all the time(which will have a big overhead).

use other ways to save your code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM