部署到Plesk的ASP.NET Core Identity应用登录后5-10秒自动登出问题求助
部署到Plesk的ASP.NET Core Identity应用登录后5-10秒自动登出问题求助
大家好,我遇到了一个特别奇怪的问题!这个问题原本出现在我的主项目里,为了复现它我特意做了个简化版的小应用,现在所有示例都来自这个小应用——因为它和主项目的问题完全一致。
我用.NET 6.0开发ASP.NET Core Web应用,创建项目时勾选了Individual Accounts和HTTPS选项,用的是ASP.NET Core Identity默认的认证配置,没加额外自定义。之后我加了Controllers文件夹,里面建了HomeController,包含Index和CreateStudent两个Action方法(就是个示例功能)。
现在问题来了:每次登录后,最多过5-10秒,只要我操作任何功能就会被踢下线,不得不反复登录。本地开发时完全没这问题,可一编译部署到Plesk主机上就出现了这个诡异的情况。我自己完全摸不着头脑,网上搜了一圈也没找到可行的解决办法,想问问大家有没有思路?谢谢啦!
我的Program.cs代码
using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using WebApplication1.Data; var builder = WebApplication.CreateBuilder(args); // Add services to the container. var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString)); builder.Services.AddDatabaseDeveloperPageExceptionFilter(); builder.Services.AddDefaultIdentity<IdentityUser>(options => { options.SignIn.RequireConfirmedAccount = true; // Diğer Identity ayarları }) .AddEntityFrameworkStores<ApplicationDbContext>(); builder.Services.ConfigureApplicationCookie(options => { options.ExpireTimeSpan = TimeSpan.FromMinutes(30); options.SlidingExpiration = true; options.Cookie.HttpOnly = true; options.Cookie.SecurePolicy = CookieSecurePolicy.Always; options.LoginPath = "/Identity/Account/Login"; options.AccessDeniedPath = "/Identity/Account/AccessDenied"; }); builder.Services.AddRazorPages(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseMigrationsEndPoint(); } else { app.UseExceptionHandler("/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.MapRazorPages(); app.MapControllerRoute( name: "specific", pattern: "Home/Index", defaults: new { controller = "Home", action = "Index" }); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run();
我的HomeController代码
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.DotNet.Scaffolding.Shared.CodeModifier.CodeChange; using WebApplication1.Data; using WebApplication1.Models; namespace WebApplication1.Controllers { [Authorize] public class HomeController : Controller { private readonly ILogger<HomeController> _logger; private readonly UserManager<IdentityUser> _userManager; private readonly SignInManager<IdentityUser> _signInManager; private readonly ApplicationDbContext _context; public HomeController(ILogger<HomeController> logger, UserManager<IdentityUser> userManager, ApplicationDbContext context, SignInManager<IdentityUser> signInManager) { _userManager = userManager; _logger = logger; _context = context; _signInManager = signInManager; } public IActionResult Index() { var students = _context.Students.ToList(); return View(students); } public IActionResult CreateStudent() { return View(); } [HttpPost] public IActionResult CreateStudent(Student model) { Student student = new Student { Name = model.Name, Surname = model.Surname, Department = model.Department }; _context.Students.Add(student); _context.SaveChanges(); return RedirectToAction("Index","Home"); } } }
补充说明(文件结构与路由)
- 我的文件结构中,Identity相关的登录页等路径是
example.com/Identity/Account/Login这类,但登录后系统会重定向到example.com/,而不是自动跳转到example.com/Home/Index,我怀疑会不会是路由配置的问题导致了登出?
已尝试的解决方案
- 调整过ApplicationCookie的配置参数(比如过期时间、滑动过期、Cookie安全策略等),但没有任何改善,问题依然存在。
内容来源于stack exchange
相关产品推荐
相关产品推荐

