我有一个ASP.NET核心网站使用AspNetCore.Identity.EntityFrameworkCore 1.1.1和cookie来授权/认证我的用户。不管我在下面的代码中选择什么作为我的设置,cookie在大约20分钟后过期,我无法弄清楚为什么。除非您关闭浏览器并清除历史记录/ cookies,否则该网站将不再起作用。有任何想法吗?ASP.NET核心身份和饼干
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块更新了我的问题。 –