.net 5 身份验证 Cookie 未设置

.net 5 Authentication Cookie not set

我有一个 Umbraco 9 .Net 5 AspNet Core 项目。

我正在尝试设置一个身份验证 cookie。我遵循了 Microsoft 指南并让它在一个单独的项目中工作,但是当试图在我的 Umbraco 项目中实现它时它失败了。我不确定为什么,但我猜 Umbraco 9 配置参与其中。

我已经在登录时在同一个控制器中获得 User.Identity.IsAuthenticated = true,但是一旦我重定向到另一个控制器,身份验证状态就为 false。

我也尝试在配置 cookie 时设置 LoginPath 选项,但它仍然重定向到默认路径 (/Account/Login),所以这里的东西也不起作用

我的 StartUp.cs 如下所示

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddUmbraco(mEnvironment, mConfig)
            .AddBackOffice()
            .AddWebsite()
            .AddComposers()
            .Build();

        services.AddDistributedMemoryCache();

        //services.AddSession(options =>
        //{
        //    options.IdleTimeout = TimeSpan.FromSeconds(10);
        //    options.Cookie.HttpOnly = true;
        //    options.Cookie.IsEssential = true;
        //});

        services.AddControllersWithViews();
        services.AddRazorPages();

        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
        { 
            options.LoginPath = "/portal/"; //not working, still redirects to default
        });

    }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseAuthentication();
        app.UseAuthorization();


        //umbraco setup 
        app.UseUmbraco()
            .WithMiddleware(u =>
            {
                u.UseBackOffice();
                u.UseWebsite();
            })
            .WithEndpoints(u =>
            {
                u.UseInstallerEndpoints();
                u.UseBackOfficeEndpoints();
                u.UseWebsiteEndpoints();
            });


        //app.UseSession();

    }

我的登录控制器操作如下所示:

public async Task<ActionResult> Login()
    {
        
        var claimsIdentity = new ClaimsIdentity(new List<Claim>
        {
            new Claim(UserClaimProperties.UserRole, MemberRole, ClaimValueTypes.String)
        }, CookieAuthenticationDefaults.AuthenticationScheme);
        var authProps = new AuthenticationProperties
        {
            ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
            IsPersistent = true,
            AllowRefresh = true,
            RedirectUri = "/"
        };

                    await HttpContext.SignInAsync(
            //CookieAuthenticationDefaults.AuthenticationScheme,  //from MS-example but isAuth will be false using this
            new ClaimsPrincipal(claimsIdentity), 
            authProps);

        var isAuthenticated = User.Identity.IsAuthenticated;

        return Redirect("/myview/");
    }

如果我在 SignInAsync 中将 Auth Scheme 设置为“Cookies”,就像在 Microsoft 示例中一样,isAuthenticated 将是 false,但如果没有这个,我至少会在这里实现它。

当重定向到下一个操作时,User.Identity.IsAuthenticated 为假。

关于为什么会这样或者为什么我的 LoginPath 配置不起作用的任何建议?

编辑: 我不想为每个登录的用户创建 Umbraco 成员。我只想让用户登录到上下文并能够验证用户在我的控制器中由我自己登录。

编辑 2: 我尝试捕捉登录事件并在其中设置了一个断点。在我的演示应用程序(没有 umbraco)中,我将到达带有 Umbraco 的那个断点,这个断点永远不会被击中。这是因为 Umbraco 可能会覆盖它或劫持事件吗?

您是否有机会阅读 Scott Brady 关于 "Umbraco frontend membership SSO using OpenID Connect"?

的文章

我已经按照 Scott 的方法成功让会员登录到我的 Umbraco v9 网站,这个方法可能对你也有帮助。

他还为 "Umbraco backoffice SSO with OpenID Connect" as well as some other very useful .NET Core specific articles 写了一篇文章,可能会对您有所帮助。

不知道为什么,但在测试不同的身份验证方案后,我收到一个错误,提示我测试的方案未注册,并且我得到了一个已注册方案的列表。 我认为通过这样做

 services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
    { 
        options.LoginPath = "/portal/"; //not working, still redirects to default
    });

我已经注册了“Cookies”方案。

其中一个已注册的方案是“Identity.Application”,通过使用该方案,我可以从重定向控制器的上下文中获取用户身份。