2017-06-12 235 views
2

我有一個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> 
} 
+0

你也可以發佈你的AddIdentity區塊嗎? –

+0

我已經使用ConfigureServices的AddIdentity塊更新了我的問題。 –

回答

2

當您使用ASPNET身份你並不需要一個單獨的CookieAuthentication中間件。 UseIdentity()將爲你做這件事,並生成一個cookie。您可以設置"cookie options"在應用程序的AddIdentity塊,像這樣:

 services.AddIdentity<ApplicationUser, IdentityRole>(config => 
      { 
       // Require a confirmed email in order to log in 
       config.SignIn.RequireConfirmedEmail = true; 

       // Your Cookie settings 
       config.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromDays(1); 
       config.Cookies.ApplicationCookie.LoginPath = "/Account/LogIn"; 
       config.Cookies.ApplicationCookie.LogoutPath = "/Account/LogOut"; 
      }).AddEntityFrameworkStores<ApplicationDbContext().AddDefaultTokenProviders(); 

而且,看看https://stackoverflow.com/a/34981457/1137785,它給這種情景的一個很好的解釋背景。

+0

謝謝@Muqeet。您的回覆幫助我解決了我的問題! –

+0

@muqeetKhan在[this](https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/identity?tabs=visual-studio%2Caspnetcore1x#tabpanel_yS0QDAFpXJ-1_aspnetcore1x)新的ASP.NET文章中正在使用以下設置選項。你的方法和文章的方法有什麼不同,你知道嗎? '//配置身份 services.Configure (選項=> {.....}' – nam

+1

@nam只是不同的擴展方法。在我的示例中,我使用的擴展方法採用'options'對象然後在該方法中的DI中配置,在你給的鏈接中,他們只是單獨做它的開放源代碼yay!在[github](https://github.com/aspnet/Identity/blob/patch /1.1.4/src/Microsoft.AspNetCore.Identity/IdentityServiceCollectionExtensions.cs) –

0

我認爲問題在於我將數據持久化爲具有不同設置的cookie。

不知道這是否是正確的方法,但我可以通過使用services.AddIdentity和app.UseCookieAuthentication來解決問題,如下所示。

在ConfigureServices,設置cookie日誌中:

 // set the cookie for sign in 
     services.AddIdentity<ApplicationUser, IdentityRole>(config => 
     {    
      // Require a confirmed email in order to log in 
      config.SignIn.RequireConfirmedEmail = true; 
      // Cookie settings 
      config.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromHours(10); 
      config.Cookies.ApplicationCookie.LoginPath = "/Account/LogIn"; 
      config.Cookies.ApplicationCookie.LogoutPath = "/Account/LogOut"; 
     }).AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders(); 

在配置設置用於保留權利要求書所述的cookie方案:

 // 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.FromSeconds(10), 
      ExpireTimeSpan = TimeSpan.FromHours(10), 
      SlidingExpiration = true, 
     }); 

在日誌中的方法中,仍然存在的權利要求:

await HttpContext.Authentication.SignInAsync(「Cookie」,userPrincipal);