首页 > 解决方案 > 在 ASP.net Core 3.1 中设置会话超时

问题描述

我有一个网络应用程序 .net core 3.1 razor pages。我将会话超时设置为 30 分钟,但是它会在 10 分钟不活动后将当前用户注销。全局 IIS 设置在服务器上设置为 60 分钟。这是我在 Startup.cs 中的代码。我错过了什么?任何帮助是极大的赞赏。谢谢!

     public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddControllers().AddNewtonsoftJson(options =>
         options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        );
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddTransient<IMemberService, MemberService>();
        services.AddTransient<INewsService, NewsService>();
        services.AddTransient<IUserCardService, UserCardService>();
        services.AddTransient<IUsernameService, UsernameService>();
        services.AddTransient<ILinkService, LinkService>();
        services.AddTransient<INotificationService, NotificationService>();

        services.AddTransient<IEmailSender, EmailSender>();
        services.Configure<AuthMessageSenderOptions>(Configuration);

        services.AddDbContext<DefaultContext>(options =>
         options.UseSqlServer(
         Configuration.GetConnectionString("DefaultContextConnection")));

        services.AddControllersWithViews()
                .AddSessionStateTempDataProvider();

        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential
            // cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            // requires using Microsoft.AspNetCore.Http;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDistributedMemoryCache();

        services.AddSession(options =>
        {
            options.IdleTimeout = TimeSpan.FromMinutes(30);
            options.Cookie.HttpOnly = true;
            options.Cookie.IsEssential = true;
            
        });

        services.AddRazorPages()
                .AddRazorRuntimeCompilation()
                .AddSessionStateTempDataProvider();

        services.AddSession();
    }

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
            //app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();

        app.UseRewriter();

        app.UseStaticFiles();

        app.UseCookiePolicy();

        app.UseRouting();
       
        app.UseAuthentication();
        app.UseAuthorization();

        app.UseSession();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();

        });



    }

标签: c#.net-coreasp.net-core-3.1session-timeout

解决方案


推荐阅读