gpt4 book ai didi

c# - Asp .net core在用户空闲时间后自动注销

转载 作者:行者123 更新时间:2023-12-02 02:15:05 27 4
gpt4 key购买 nike

我正在使用带有 MVC 的 dot net core 2.0。我需要实现这个功能。如果用户闲置 15 分钟,我需要刷新并重定向到登录页面。我使用了声明身份验证。这是我在 starup.cs 中尝试过的

services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
//options.Cookie.Expiration = TimeSpan.FromDays(150);
options.ExpireTimeSpan = TimeSpan.FromSeconds(15);
options.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
options.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
options.SlidingExpiration = true;
});

“options.ExpireTimeSpan = TimeSpan.FromSeconds(15);”我认为这将帮助我在 15 秒后注销(出于测试目的,实际上是 15 分钟)。

这是我的整个启动过程

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

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

services.AddIdentity<ApplicationUser, ApplicationRole>(config =>
{
config.SignIn.RequireConfirmedEmail = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddScoped<UserManager<ApplicationUser>>();
services.Configure<IdentityOptions>(options =>
{
// Password settings
options.Password.RequireDigit = true;
options.Password.RequiredLength = 8;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = true;
options.Password.RequireLowercase = false;
options.Password.RequiredUniqueChars = 6;

// Lockout settings
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
options.Lockout.MaxFailedAccessAttempts = 10;
options.Lockout.AllowedForNewUsers = true;

// User settings
options.User.RequireUniqueEmail = true;

});
services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
//options.Cookie.Expiration = TimeSpan.FromDays(150);
options.ExpireTimeSpan = TimeSpan.FromSeconds(15);
options.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
options.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
options.SlidingExpiration = true;
});


services.Configure<EmailSettings>(Configuration.GetSection("EmailSettings"));

// Add application services.
services.AddTransient<IEmailSender, EmailSender>();

//Common Services
services.AddTransient<CommonService, CommonService>();
services.AddMvc()
.AddJsonOptions(options =>
options.SerializerSettings.ContractResolver = new DefaultContractResolver());

services.Configure<AppSettings>(Configuration.GetSection("ApplicationSettings"));
// Add Kendo UI services to the services container
services.AddKendo();

//Date Format
services.Configure<DateSettings>(Configuration.GetSection("DateSettings"));

//Templates
services.Configure<Templates>(Configuration.GetSection("Templates"));

//Themes
services.Configure<ThemeSettings>(Configuration.GetSection("ThemeSettings"));

//Title
services.Configure<TitleSettings>(Configuration.GetSection("TitleSettings"));

//Google reCaptcha
services.Configure<GoogleReCaptcha>(Configuration.GetSection("GoogleReCaptcha"));

services.Configure<LoginAttemptsToCaptcha>(Configuration.GetSection("LoginAttemptsToCaptcha"));
services.Configure<PhysicalExamination>(Configuration.GetSection("PhysicalExamination"));

//Reset Password Settings
//var reset = services.Configure<ResetPasswordSettings>(Configuration.GetSection("ResetPasswordSettings"));
var resetsettingsSection = Configuration.GetSection("ApplicationSettings");
var settings = resetsettingsSection.Get<AppSettings>();

services.Configure<DataProtectionTokenProviderOptions>(options =>
{
options.TokenLifespan = TimeSpan.FromMinutes(settings.ResetPasswordExpiryTime);
});

//services.AddMvc().AddSessionStateTempDataProvider();
//services.AddSession();
//services.AddSession(options =>
//{
// options.IdleTimeout = TimeSpan.FromSeconds(10);
//});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
UserManager<ApplicationUser> userManager,
RoleManager<ApplicationRole> roleManager, ApplicationDbContext context)

{

//app.UseMiddleware<AuthenticationMiddleware>();
//app.UseMiddleware<ErrorHandlingMiddleware>();
app.UseAuthenticationMiddleware();
if (env.IsDevelopment())
{
//app.UseBrowserLink();
//app.UseDeveloperExceptionPage();
//app.UseDatabaseErrorPage();
//app.UseExceptionHandler("/Home/Error");
}
else
{
//app.UseExceptionHandler("/Home/Error");
}

app.UseStaticFiles();

app.UseAuthentication();

using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
if (!serviceScope.ServiceProvider.GetService<ApplicationDbContext>().AllMigrationsApplied())
{
serviceScope.ServiceProvider.GetService<ApplicationDbContext>().Database.Migrate();
}
AppIdentityDataInitializer.SeedAdminUser(userManager, roleManager, context);
serviceScope.ServiceProvider.GetService<ApplicationDbContext>().EnsureSeeded();
}

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
// Configure Kendo UI
//app.UseKendo(env);

//app.UseSession();
}
}

任何人都可以帮助我实现这一目标吗?

最佳答案

如果你希望该页面在空闲时自动注销用户,你必须添加一些js代码。它的目的是跟踪中间时间,如果它比注销操作长于 15 秒。最简单,重定向到注销操作。更奇特的是,ajax 调用注销并响应显示登录模式。Cookie 设置可以调整为有效时间超过 15 秒。想象一下,您希望在空闲时间更长的情况下拥有页面,但通过在 cookie 中严格设置它,您无法实现这一点。

关于c# - Asp .net core在用户空闲时间后自动注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52773642/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com