From de3ee2348df8bc24ee421e67d222ff2a4e6108ec Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 20 Jun 2025 12:49:35 +0800 Subject: [PATCH] Update `hangfire.md` --- .../background-jobs/hangfire.md | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/docs/en/framework/infrastructure/background-jobs/hangfire.md b/docs/en/framework/infrastructure/background-jobs/hangfire.md index f5991ee19b..da0e3953ab 100644 --- a/docs/en/framework/infrastructure/background-jobs/hangfire.md +++ b/docs/en/framework/infrastructure/background-jobs/hangfire.md @@ -190,18 +190,20 @@ private void ConfigureAuthentication(ServiceConfigurationContext context, IConfi options.Authority = configuration["AuthServer:Authority"]; options.RequireHttpsMetadata = configuration.GetValue("AuthServer:RequireHttpsMetadata"); options.Audience = "MyProjectName"; - }); - context.Services.AddAuthentication() - .AddCookie("Cookies") - .AddOpenIdConnect("oidc", options => + options.ForwardDefaultSelector = httpContext => httpContext.Request.Path.StartsWithSegments("/hangfire", StringComparison.OrdinalIgnoreCase) + ? CookieAuthenticationDefaults.AuthenticationScheme + : null; + }) + .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme) + .AddAbpOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options => { options.Authority = configuration["AuthServer:Authority"]; - options.RequireHttpsMetadata = configuration.GetValue("AuthServer:RequireHttpsMetadata"); - options.ResponseType = OpenIdConnectResponseType.CodeIdToken; + options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]); + options.ResponseType = OpenIdConnectResponseType.Code; - options.ClientId = configuration["AuthServer:ClientId"]; - options.ClientSecret = configuration["AuthServer:ClientSecret"]; + options.ClientId = configuration["AuthServer:HangfireClientId"]; + options.ClientSecret = configuration["AuthServer:HangfireClientSecret"]; options.UsePkce = true; options.SaveTokens = true; @@ -211,6 +213,8 @@ private void ConfigureAuthentication(ServiceConfigurationContext context, IConfi options.Scope.Add("email"); options.Scope.Add("phone"); options.Scope.Add("MyProjectName"); + + options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; }); } ``` @@ -218,26 +222,27 @@ private void ConfigureAuthentication(ServiceConfigurationContext context, IConfi ```csharp app.Use(async (httpContext, next) => { - if (httpContext.Request.Path.StartsWithSegments("/hangfire")) + if (httpContext.Request.Path.StartsWithSegments("/hangfire", StringComparison.OrdinalIgnoreCase)) { - var result = await httpContext.AuthenticateAsync("Cookies"); - if (result.Succeeded) + var authenticateResult = await httpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme); + if (!authenticateResult.Succeeded) { - httpContext.User = result.Principal; - await next(httpContext); + await httpContext.ChallengeAsync( + OpenIdConnectDefaults.AuthenticationScheme, + new AuthenticationProperties + { + RedirectUri = httpContext.Request.Path + httpContext.Request.QueryString + }); return; } - - await httpContext.ChallengeAsync("oidc"); - } - else - { - await next(httpContext); } + await next.Invoke(); }); - app.UseAbpHangfireDashboard("/hangfire", options => { - options.AsyncAuthorization = new[] {new AbpHangfireAuthorizationFilter()}; + options.AsyncAuthorization = new[] + { + new AbpHangfireAuthorizationFilter() + }; }); ```