使用Identity到Windows 2008 R2(IIS 7.5)部署ASP.Net Core应用程序后,我在大约20分钟不活动后仍无法保持登录状态。我只使用简单的用户名/密码验证,没有第三方的东西。
这不是运行VS 2017的Dev机器上的问题。
AccountController.cs
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, true, lockoutOnFailure: false);
if (result.Succeeded)
{
_logger.LogInformation(1, "User logged in.");
return RedirectToLocal(returnUrl);
}
if (result.RequiresTwoFactor)
{
return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
}
if (result.IsLockedOut)
{
_logger.LogWarning(2, "User account locked out.");
return View("Lockout");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return View(model);
}
}
return View(model);
}
任何帮助都很感激。
修改 这是我的ConfigureServices方法的全部内容。它有一个可能相关的授权政策。
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddAuthorization(options =>
{
options.AddPolicy("UserOnly", policy => policy.RequireRole("User"));
options.AddPolicy("InstructorOnly", policy => policy.RequireRole("Instructor"));
options.AddPolicy("AdminOnly", policy => policy.RequireRole("Admin"));
options.AddPolicy("SystemManagerOnly", policy => policy.RequireRole("Manager"));
});
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
// Configure Identity
services.Configure<IdentityOptions>(options =>
{
// Password settings
options.Password.RequireDigit = true;
options.Password.RequiredLength = 8;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
// Lockout settings
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(240);
options.Lockout.MaxFailedAccessAttempts = 10;
// Cookie settings
options.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromDays(15);
options.Cookies.ApplicationCookie.LoginPath = "/Account/LogIn";
options.Cookies.ApplicationCookie.LogoutPath = "/Account/LogOff";
options.Cookies.ApplicationCookie.SlidingExpiration = true;
// User settings
options.User.RequireUniqueEmail = true;
});
var appSettings = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettings);
// Inject the ability to get current user
services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();
// Inject the User access class
services.AddTransient<UserResolverService>();
}
以下是Chrome开发工具的屏幕截图,其中显示了响应/请求Cookie。登录页面似乎发送了正确的cookie,其中包含将来15天的到期日期。使用此cookie调用后续页面。
答案 0 :(得分:1)
_signInManager.PasswordSignInAsync 会像这样创建一个Cookie
Set-Cookie: .AspNetCore.Identity.Application=<cookie value>; expires=Fri, 14 Jul 2017 02:59:56 GMT; path=/; httponly
下次当你提出请求时,如果它在到期时间戳内,它应该发送这个cookie。你的情况会发生什么
您是否有一些自定义授权policy?同样在您的应用程序中,您是在会话和/或任何全局变量中存储任何内容,然后在登录时检查它吗?
20分钟是IIS的默认IdleTimeout.Whenever任何工作者 进程空闲超过20分钟,IIS关闭进程 当下一个请求到来时,它会自动引入工作进程 您可以configure更高的超时或应用程序池的无限超时
答案 1 :(得分:0)
由于一些非常奇怪的原因,我不再看到这个问题了。
我对配置或代码所做的唯一更改是配置IIS版本日志以启用cs(Cookie)。
感谢您的帮助@Rohith Rajan