我已在 ASP.NET Core Blazor 应用程序中成功设置了 Azure AD B2C 身份验证。我可以在多个选项卡中打开网站 (https://localhost:5001),而无需再次登录。但是,如果我保持服务器运行但关闭并重新打开浏览器并导航到该网站,则需要我再次登录。我的理解是,它应该让我在浏览器 session 之间保持登录状态。我对这一切都很陌生,所以我什至不知道从哪里开始寻找问题。有什么想法吗?
这是我的 Startup.cs
,如果有帮助的话。
public class Startup
{
private IConfiguration Configuration { get; }
public Startup(IConfiguration configuration) => Configuration = configuration;
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAdB2C"));
services.AddControllersWithViews().AddMicrosoftIdentityUI();
services.AddAuthorization(options =>
{
// By default, all incoming requests will be authorized according to the default policy
options.FallbackPolicy = options.DefaultPolicy;
});
services.AddRazorPages();
services.AddServerSideBlazor().AddMicrosoftIdentityConsentHandler();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/_Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
最佳答案
我找到了解决问题的方法。感谢@huysentruitw让我走上正确的道路。
TL;DR:创建我自己的 AccountController
和设置AuthenticationProperties.IsPersistent
至true
登录时修复了身份验证 cookie 的过期问题,最终解决了我的问题。
问题基本上是身份验证 token cookie 没有在浏览器 session 中保留。它始终会创建 cookie,但在 DevTools 中查看,过期时间始终设置为“Session”。但是,可以通过 Startup.cs
中提供的选项配置过期时间。也没有解决。我尝试了以下方法:
- 通过
ConfigureApplicationCookie
设置与CookieAuthenticationOptions.Cookie.Expires
. - 通过
ConfigureApplicationCookie
设置与CookieAuthenticationOptions.ExpireTimeSpan
- 通过
.AddMicrosoftIdentityWebApp
设置使用重载来提供configureCookieAuthenticationOptions
与CookieBuilder.Expires
属性(property)已填充。 - 还有一些我没想到能发挥作用的其他晦涩的东西,但我一直在寻找有用的东西。
无论我做什么,cookie 在“ session ”后都会过期。
因此,我进入了 ASP.Net Core 身份验证代码( CookieAuthenticationHandler
中的 Microsoft.AspNetCore.Authentication.Cookies
类)来找出为什么没有使用过期值。我发现CookieOptions.Expires
由于某种原因被覆盖(在 BuildCookieOptions
方法中),并且仅在 AuthenticationProperties.IsPersistent
时才设置到期日是 true
(您可以在 HandleSignInAsync
方法中看到这种情况发生)。所以,我的下一步是弄清楚如何将其设置为 true。我发现AuthenticationProperties
由 AccountController
设置由 Startup.cs
中的调用自动添加至.AddMicrosoftIdentityUI
。我复制了这个AccountController
以我的项目作为起点,并摆脱了对 .AddMicrosoftIdentityUI
的调用。然后我更新了AuthenticationProperties
设置IsPersistent
至true
,这最终解决了问题。现在,cookie 带有到期日期/时间。
我不知道我是否错过了一些东西,但这对我来说确实像是一个错误,或者至少是一个非常糟糕的配置体验。希望有人能指出我的错误,但这对我有用。我认为拉 AccountController
更有意义无论如何,我都可以进入我的项目,以便我可以看到正在发生的事情并根据需要对其进行微调。
关于c# - 浏览器关闭后 ASP.NET Core Auth 不会保持登录状态 (Azure AD B2C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65451797/