首页 > 解决方案 > 浏览器关闭后 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");
        });
    }

标签: c#asp.net-coreauthenticationblazorazure-ad-b2c

解决方案


我找到了解决我的问题的方法。感谢@huysentruitw让我走上了正确的道路。

TL;DR:创建我自己的AccountController并设置AuthenticationProperties.IsPersistenttrue登录时修复了身份验证 cookie 的过期问题,最终解决了我的问题。

问题基本上是身份验证令牌cookie没有在浏览器会话中保留。它总是会创建 cookie,但在 DevTools 中查看,过期总是设置为“Session”。但是,通过 中可用的选项配置过期时间Startup.cs也没有解决它。我尝试了以下方法:

  • ConfigureApplicationCookie通过设置它CookieAuthenticationOptions.Cookie.Expires
  • ConfigureApplicationCookie通过with设置它CookieAuthenticationOptions.ExpireTimeSpan
  • .AddMicrosoftIdentityWebApp通过使用重载来设置它以提供填充configureCookieAuthenticationOptions的属性。CookieBuilder.Expires
  • 其他一些我没想到会起作用的晦涩难懂的东西,但我一直在寻找可以起作用的东西。

无论我做什么,cookie 在“会话”之后过期。

因此,我进入了 ASP.Net Core 身份验证代码(中的CookieAuthenticationHandlerMicrosoft.AspNetCore.Authentication.Cookies)以找出未使用过期值的原因。我发现CookieOptions.Expires由于某种原因(在BuildCookieOptions方法中)它被覆盖了,并且只有在设置时才设置到期AuthenticationProperties.IsPersistenttrue您可以在方法中看到这种情况HandleSignInAsync)。因此,我的下一步是弄清楚如何将其设置为 true。我发现它们AuthenticationPropertiesAccountController由调用自动添加Startup.cs.AddMicrosoftIdentityUI。我将其复制AccountController到我的项目中作为起点,并摆脱了对.AddMicrosoftIdentityUI. AuthenticationProperties然后我将设置更新IsPersistenttrue,这最终解决了这个问题。cookie 现在带有过期日期/时间。

我不知道我是否遗漏了什么,但这对我来说肯定是一个错误,或者至少是一个非常糟糕的配置体验。希望有人会出现并可能指出我的错误,但这对我有用。我认为无论如何将其拉入我的项目更有意义,AccountController这样我就可以看到正在发生的事情并根据需要对其进行微调。


推荐阅读