【发布时间】:2017-06-12 13:18:58
【问题描述】:
我有一个 ASP.NET Core 站点,它使用 AspNetCore.Identity.EntityFrameworkCore 1.1.1 和 cookie 来授权/验证我的用户。无论我在下面的代码中选择什么设置,cookie 都会在大约 20 分钟后过期,我不知道为什么。除非您关闭浏览器并清除历史记录/cookie,否则该网站将不再工作。有什么想法吗?
services.AddIdentity<ApplicationUser, IdentityRole>(config =>
{
// Require a confirmed email in order to log in
config.SignIn.RequireConfirmedEmail = true;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
app.UseIdentity();
// Add cookie middleware to the configure an identity request and persist it to a cookie.
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookie",
LoginPath = new PathString("/Account/Login/"),
AccessDeniedPath = new PathString("/Account/Forbidden/"),
AutomaticAuthenticate = true,
AutomaticChallenge = true,
ExpireTimeSpan = TimeSpan.FromMinutes(20),
SlidingExpiration = true,
});
我还有一些剃须刀代码控制是否在 _layout 页面上显示管理菜单。当 cookie 过期时,这会崩溃,因为用户突然没有声明。有没有更好的方法来处理这个问题?
// If user is admin then show drop down with admin navigation
@if (User.HasClaim(System.Security.Claims.ClaimTypes.Role, "admin"))
{
<ul class="nav navbar-nav">
@*etc*@
</ul>
}
【问题讨论】:
-
您能否也发布您的 AddIdentity 部分块?
-
我已经用 ConfigureServices 的 AddIdentity 块更新了我的问题。
标签: session cookies asp.net-core asp.net-core-identity