Browse Source

Update `hangfire.md`

pull/23118/head
maliming 1 year ago
parent
commit
de3ee2348d
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 47
      docs/en/framework/infrastructure/background-jobs/hangfire.md

47
docs/en/framework/infrastructure/background-jobs/hangfire.md

@ -190,18 +190,20 @@ private void ConfigureAuthentication(ServiceConfigurationContext context, IConfi
options.Authority = configuration["AuthServer:Authority"]; options.Authority = configuration["AuthServer:Authority"];
options.RequireHttpsMetadata = configuration.GetValue<bool>("AuthServer:RequireHttpsMetadata"); options.RequireHttpsMetadata = configuration.GetValue<bool>("AuthServer:RequireHttpsMetadata");
options.Audience = "MyProjectName"; options.Audience = "MyProjectName";
});
context.Services.AddAuthentication() options.ForwardDefaultSelector = httpContext => httpContext.Request.Path.StartsWithSegments("/hangfire", StringComparison.OrdinalIgnoreCase)
.AddCookie("Cookies") ? CookieAuthenticationDefaults.AuthenticationScheme
.AddOpenIdConnect("oidc", options => : null;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddAbpOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
{ {
options.Authority = configuration["AuthServer:Authority"]; options.Authority = configuration["AuthServer:Authority"];
options.RequireHttpsMetadata = configuration.GetValue<bool>("AuthServer:RequireHttpsMetadata"); options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]);
options.ResponseType = OpenIdConnectResponseType.CodeIdToken; options.ResponseType = OpenIdConnectResponseType.Code;
options.ClientId = configuration["AuthServer:ClientId"]; options.ClientId = configuration["AuthServer:HangfireClientId"];
options.ClientSecret = configuration["AuthServer:ClientSecret"]; options.ClientSecret = configuration["AuthServer:HangfireClientSecret"];
options.UsePkce = true; options.UsePkce = true;
options.SaveTokens = true; options.SaveTokens = true;
@ -211,6 +213,8 @@ private void ConfigureAuthentication(ServiceConfigurationContext context, IConfi
options.Scope.Add("email"); options.Scope.Add("email");
options.Scope.Add("phone"); options.Scope.Add("phone");
options.Scope.Add("MyProjectName"); options.Scope.Add("MyProjectName");
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}); });
} }
``` ```
@ -218,26 +222,27 @@ private void ConfigureAuthentication(ServiceConfigurationContext context, IConfi
```csharp ```csharp
app.Use(async (httpContext, next) => 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"); var authenticateResult = await httpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
if (result.Succeeded) if (!authenticateResult.Succeeded)
{ {
httpContext.User = result.Principal; await httpContext.ChallengeAsync(
await next(httpContext); OpenIdConnectDefaults.AuthenticationScheme,
new AuthenticationProperties
{
RedirectUri = httpContext.Request.Path + httpContext.Request.QueryString
});
return; return;
} }
await httpContext.ChallengeAsync("oidc");
}
else
{
await next(httpContext);
} }
await next.Invoke();
}); });
app.UseAbpHangfireDashboard("/hangfire", options => app.UseAbpHangfireDashboard("/hangfire", options =>
{ {
options.AsyncAuthorization = new[] {new AbpHangfireAuthorizationFilter()}; options.AsyncAuthorization = new[]
{
new AbpHangfireAuthorizationFilter()
};
}); });
``` ```

Loading…
Cancel
Save