我有一個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塊更新了我的問題。 –