0

我目前正在使用ASP.NET核心身份。我無法弄清楚延長會話長度的設置,但我一直在註銷 - 我假設有一個大約20分鐘的失效時間,但我找不到該設置。請注意,我將Google用作外部OAuth。ASP.NET核心身份到期(谷歌OAuth)

 services.AddIdentity<ApplicationUser, IdentityRole>(o => 
      { 
       o.Password.RequireDigit = false; 
       o.Password.RequireLowercase = false; 
       o.Password.RequireUppercase = false; 
       o.Password.RequireNonAlphanumeric = false; 
       o.Password.RequiredLength = 6; 
       o.SecurityStampValidationInterval = TimeSpan.FromHours(8); 
       o.Cookies.ExternalCookie.ExpireTimeSpan = TimeSpan.FromHours(8); 
       o.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromHours(8); 
      }) 
      .AddEntityFrameworkStores<ApplicationDbContext>() 
      .AddDefaultTokenProviders(); 


     app.UseIdentityServer(); 

     app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions 
     { 
      Authority = $"http://localhost:55504/", 
      RequireHttpsMetadata = false, 
      AllowedScopes = 
      { 
       IdentityServerConstants.StandardScopes.OpenId, 
       IdentityServerConstants.StandardScopes.Profile, 
       IdentityServerConstants.StandardScopes.Email, 
       "name", 
       "given_name", 
       "family_name", 
       "role" 
      } 
     }); 

     var googleOptions = serviceProvider.GetRequiredService<GoogleOptions>(); 
     app.UseGoogleAuthentication(new GoogleOptions 
     { 
      AuthenticationScheme = "Google", 
      SignInScheme = "Identity.External", 
      ClientId = googleOptions.ClientId, 
      ClientSecret = googleOptions.ClientSecret 
     }); 
+0

可能的複製[Cookie身份驗證在ASP.NET Core中過期過早](https://stackoverflow.com/questions/45595615/c ookie-authentication-expiring-too-soon-in-asp-net-core) – SteelToe

+0

你在哪裏以及如何託管你的應用程序? IIS? Azure應用服務?然後,您需要啓用數據保護,因此您的Cookie的加密密鑰在應用程序重新啓動後仍然存在。請參閱[我的答案](https://stackoverflow.com/a/47559544/455493) – Tseng

回答

0

這個問題\答案是特定於Identity Server的4

你會做這樣的事情在你的配置:

app.UseGoogleAuthentication(new GoogleOptions 
{ 
    SignInScheme = "Identity.External", // this is the name of the cookie middleware registered by UseIdentity() 
    ClientId = Configuration["ExternalAuthentication:Google:ClientId"], 
    ClientSecret = Configuration["ExternalAuthentication:Google:ClientSecret"] 
}); 

app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions 
{ 
    Authority = $"http://localhost:55504/", 
    RequireHttpsMetadata = false, 
    AllowedScopes = 
    { 
     IdentityServerConstants.StandardScopes.OpenId, 
     IdentityServerConstants.StandardScopes.Profile, 
     IdentityServerConstants.StandardScopes.Email, 
     "name", 
     "given_name", 
     "family_name", 
     "role" 
    } 
     // CookieLifetime default is 10 Hours 
     Authentication.CookieLifetime = TimeSpan.FromHours(24); 

     // Default CookieSlidingExpiration = false; 
     Authentication.CookieSlidingExpiration = true; 
}); 

,並在您ConfigureServices

// Identity 
    // https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity 
    // http://docs.identityserver.io/en/release/quickstarts/6_aspnet_identity.html 
    services.AddIdentity<ApplicationUser, IdentityRole>(o => { 
      // configure identity options 
      o.Password.RequireDigit = false; 
      o.Password.RequireLowercase = false; 
      o.Password.RequireUppercase = false; 
      o.Password.RequireNonAlphanumeric = false; 
      o.Password.RequiredLength = 6; 
     }) 
      .AddEntityFrameworkStores<AuthDbContext>() 
      .AddDefaultTokenProviders(); 
+0

當應用程序回收並且加密密鑰丟失並且在下次啓動時生成新的加密密鑰時,這將無濟於事 – Tseng

+0

I坦率地說,袖口答案。但是,我將使用當前正在服務器重新啓動之間工作的代碼更新我的答案,使用MS Identity和IdentityServer 4託管在Azure上的Google OAuth。名稱「Idenity」是其代碼中2個獨立軟件包的一部分。 – ttugates

+0

通過來自IS4的令牌工作流程不會發生此問題,它通過OOB AccountController和CookieAuthentication - 我將嘗試禁用IS4並查看錯誤是否仍然存在。 –