会话期满后如何自动重定向到其他页面?
下面是我的startup.cs代码。
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddAuthentication(options => {
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
}).AddCookie(options =>
{
options.LoginPath = "/auth/signin";
options.SlidingExpiration = true;
options.ExpireTimeSpan = TimeSpan.FromSeconds(30);
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc();
}
如果我单击按钮或Actionlink,就可以重定向到登录页面。 但是我想在会话过期时自动重定向到其他页面(登录页面除外)。 有人可以帮我吗?
答案 0 :(得分:0)
尝试一下:
options.Cookie.Name = "auth_cookie";
options.Cookie.SameSite = SameSiteMode.None;
options.LoginPath = new Microsoft.AspNetCore.Http.PathString("/Account/Login");
options.AccessDeniedPath = "/Account/Login";
options.ReturnUrlParameter = "/Account/Login";
options.AccessDeniedPath = "/Account/AccessDenied";
options.LogoutPath = "/Account/Logout";
options.ExpireTimeSpan = TimeSpan.FromMinutes(1);//set it less for testing purpose
// options.Events.OnRedirectToLogin
注释掉代码Events.OnRedirectToLogin或OnRedirectToLogin的代码,这将返回错误代码。 我认为我们可以显示错误401或302等,也可以将用户重定向到“登录”页面。 希望对您有帮助