使用 OpenID Connect 注销后强制登录并在 Blazor (.Net 6) 上设置生命周期 cookie

Force Login after logout with OpenID Connect and set lifespan cookie on Blazor (.Net 6)

我将 Blazor 与 .Net 6 服务器一起使用,在使用 OpenID 连接到 ADFS 进行身份验证后,我在注销和 cookie 方面遇到了一些问题。

第一次登录完美运行(我看到了 adfs 页面)。问题是当用户注销时,使用自定义注销页面,如果用户尝试访问受保护页面(注销后),它无需登录即可再次访问。几秒钟后,在地址栏中,它显示了 ADFS 的调用,然后进行了身份验证,但没有显示任何用户名和密码形式。我如何强制用户在注销后每次都登录 ADFS?

对于此配置,我遵循以下示例:Blazor OpenID-Connect

另一件事是Cookies的时间跨度。我在 .AddCookie() 方法中尝试了一些解决方案,但没有成功。如何正确设置 Cookies 时间跨度(10 分钟)?

最后一题是/.well-known/openid-configuration。在从 Duende 身份服务器更改为这个简单的解决方案(下面的代码)之前,我无法到达此端点。为什么?

Setup.cs 文件:

public void ConfigureServices(IServiceCollection services)
{
   ...
   services.AddAuthentication(opt =>
   {
     opt.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
     opt.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
     opt.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
   })
     .AddCookie()
     .AddOpenIdConnect("oidc", options =>
     {
       options.Authority = "https://adfs......./adfs";
       options.ClientId = "<my_ID>";
       options.ClientSecret = "<my_Secret>";

       options.SignInScheme = "Cookies";
       options.RequireHttpsMetadata = false;

       options.ResponseType = "id_token";
       options.SaveTokens = true;
       options.GetClaimsFromUserInfoEndpoint = true;

       options.UseTokenLifetime = false;

       options.Scope.Add("openid");
       options.Scope.Add("profile");
       options.TokenValidationParameters = new TokenValidationParameters { NameClaimType = "name" };
       options.Events = new OpenIdConnectEvents
       {
         OnAccessDenied = context =>
         {
           context.HandleResponse();
           context.Response.Redirect("/");
           return Task.CompletedTask;
         }
       };
    });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
  ...
  app.UseAuthentication();
  app.UseAuthorization();
}

Logout.cshtml

@page
@model Service.Pages.LogoutModel
@{
}

Logout.cshtml.cs

namespace Service.Pages
{
    public class LogoutModel : PageModel
    {
        public async Task<IActionResult> OnGetAsync()
        {
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            await HttpContext.SignOutAsync("Cookies");
            await HttpContext.SignOutAsync("oidc");
            return Redirect("/");
        }
    }
}

我解决了关于 /.well-know 端点的问题。我弄错了服务器名,现在可以正常工作了。当您使用 Duende Identity Server 时,此配置页面与您的项目相同(例如 https://localhost:5001/.well-know/openid-configuration),但没有此路径是:

https://[your-adfs-server]/adfs/.well-known/openid-configuration

options.Authority.

配置相同

现在仍然不确定为什么注销不能正常工作.. 可能是 ADFS 的问题。