[英]How to set Hangfire authorization in F#?
根據Hangire 文檔,可以在 C# 中通過以下方式允許對 Hangfire 儀表板的授權:
// For ASP.NET Core environments, use the GetHttpContext extension method defined in the Hangfire.AspNetCore package.
public class MyAuthorizationFilter : IDashboardAuthorizationFilter
{
public bool Authorize(DashboardContext context)
{
var httpContext = context.GetHttpContext();
// Allow all authenticated users to see the Dashboard (potentially dangerous).
return httpContext.User.Identity.IsAuthenticated;
}
}
// The second step is to pass it to the UseHangfireDashboard method. You can pass multiple filters, and the access will be granted only if all of them return true.
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
Authorization = new [] { new MyAuthorizationFilter() }
});
我使用 Giraffe 作為網絡服務器,我嘗試了以下方法:
type HangfireAuthorization () =
interface IDashboardAuthorizationFilter with
override _.Authorize (dc: DashboardContext) =
false
type WebApp(context: StatelessServiceContext) =
inherit StatelessService(context)
//....
let configureApp (app: IApplicationBuilder) =
let storageContext = app.ApplicationServices.GetService(typeof<StorageContext>) :?> StorageContext
let hangfireDashboardOptions: DashboardOptions =
let x = DashboardOptions()
x.Authorization <- HangfireAuthorization() // how to correctly set this option?
x
app.UseGiraffeErrorHandler(errorHandler)
.UseCors("AllowCors")
.UseHangfireDashboard("/hangfire", hangfireDashboardOptions)
如何正確設置 Hangfire DashboardOptions 的授權字段?
我讓它像這樣工作:
type HangfireAuthorization () =
interface IDashboardAuthorizationFilter with
override _.Authorize (dc: DashboardContext) =
false
let hangfireDashboardOptions: DashboardOptions =
let x = DashboardOptions()
x.Authorization <- [ HangfireAuthorization() ] // or [| HangfireAuthorization() |]
x
app.UseGiraffeErrorHandler(errorHandler)
.UseCors("AllowCors")
.UseHangfireDashboard("/hangfire", hangfireDashboardOptions)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.