c# - 浏览器关闭后 ASP.NET Core Auth 未保持登录状态(Azure AD B2C)
问题描述
我已在我的 ASP.NET Core Blazor 应用程序中成功设置了 Azure AD B2C 身份验证。我可以在多个选项卡中打开网站 (https://localhost:5001) 而无需再次登录。但是,如果我保持服务器运行但关闭并重新打开浏览器并导航到该网站,则需要我再次登录。我的理解是它应该让我在浏览器会话之间保持登录状态。我对这一切都很陌生,所以我什至不确定从哪里开始寻找问题。任何想法这可能是什么?
这是我的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 的过期问题,最终解决了我的问题。
问题基本上是身份验证令牌cookie没有在浏览器会话中保留。它总是会创建 cookie,但在 DevTools 中查看,过期总是设置为“Session”。但是,通过 中可用的选项配置过期时间Startup.cs
也没有解决它。我尝试了以下方法:
ConfigureApplicationCookie
通过设置它CookieAuthenticationOptions.Cookie.Expires
。ConfigureApplicationCookie
通过with设置它CookieAuthenticationOptions.ExpireTimeSpan
.AddMicrosoftIdentityWebApp
通过使用重载来设置它以提供填充configureCookieAuthenticationOptions
的属性。CookieBuilder.Expires
- 其他一些我没想到会起作用的晦涩难懂的东西,但我一直在寻找可以起作用的东西。
无论我做什么,cookie 在“会话”之后过期。
因此,我进入了 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
这样我就可以看到正在发生的事情并根据需要对其进行微调。
推荐阅读
- npm - 升级 yarn 似乎可以工作,但没有,安装了 nvm
- sql - 前一个分区 ID 更改时重置 ROW_NUMBER()
- react-native - 如何将手势响应系统与 Pressable 的 pressretentionoffset 道具结合使用?
- javascript - 悬停时的Javascript停止循环?
- azure - azure 上的应用服务已关闭。对我来说没有意义的多个错误
- c++ - 使用 spdlog 时“无法格式化参数”
- javascript - 使用 discord.js 和 ytdl API 通过机器人播放音乐时的性能问题
- javascript - 如何使 2 个函数 onclick 在 JS 中工作?
- css - 显示`的内容`当`
` 已关闭
- database - 包含有关数据库信息的 Sqlite 显示表