Hangfire 仪表板的更复杂的身份验证

More Complicated Authentication for Hangfire Dashboard

我在应用程序上使用 hangfire,我需要使用我的存储库进行仪表板身份验证。为此,我必须在 Hangfire 的 Authorize 方法中解析存储库,但是使用 OwinContext 我无法这样做。我选择为这个项目使用 SimpleInjector,因为它在 WebApiConfigs Register 方法期间注册了所有内容,所以我想达到它。最近我将 middleware 用作 MessageHandler,由此,我成功地使用 HttpRequestMessage 解决了依赖关系。但是在 OwinContext 我无法访问它并使用 HttpRequestMessage.GetDependencyScope().

通过它解决依赖关系

这就是 Hangfire 建议在其文档中为 Asp.net 应用程序进行身份验证的方式;

public class MyAuthorizationFilter : IDashboardAuthorizationFilter
{
    public bool Authorize(DashboardContext context)
    {
        // In case you need an OWIN context, use the next line, `OwinContext` class
        // is the part of the `Microsoft.Owin` package.
        var owinContext = new OwinContext(context.GetOwinEnvironment());

        // Allow all authenticated users to see the Dashboard (potentially dangerous).
        return owinContext.Authentication.User.Identity.IsAuthenticated;
    }
}

因为我在前端使用 Angular 这个 owinContext.Authentication.User 是空的。即使不是,我也只想让自己接触到仪表板。所以这不能解决我的问题。

如何在 Authorize 方法中解决我的依赖关系?

我不能通过构造函数注入来做到这一点,因为对于 hangfire,你在 Startup.css Configuration 上说 UseHangfireDashboard 如下所述;

这是我的 Startup.cs 文件

private IEnumerable<IDisposable> GetHangfireServers()
{
    Hangfire.GlobalConfiguration.Configuration
        .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
        .UseSimpleAssemblyNameTypeSerializer()
        .UseRecommendedSerializerSettings()
        .UseSqlServerStorage("CONN_STR", new SqlServerStorageOptions
        {
            CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
            SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
            QueuePollInterval = TimeSpan.Zero,
            UseRecommendedIsolationLevel = true,
            DisableGlobalLocks = true
        });

    yield return new BackgroundJobServer();
}

public void Configuration(IAppBuilder app)
{
    app.UseHangfireAspNet(GetHangfireServers);
    app.UseHangfireDashboard("/hangfire", new DashboardOptions { 
        Authorization = new [] { 
            new DashboardAuthorization() 
            /* explanation */ 
            // for DI to work through constructor, 
            //I have to give my AuthRepository as a parameter here. 
            //And my AuthRepository also has many DIs so, 
            //it's not possible through here.
        }
    });

    BackgroundJob.Enqueue(() => Console.WriteLine("Hello world from Hangfire!"));

}

顺便说一句,我的项目是.Net Framework 4.7.2项目。

就 Hangfire 而言,您负责提供实现 IDashboardAuthorizationFilter 的 class 实例。因此,如果 class 具有您要注入的依赖项,则需要您进行连接。最好在 DI 容器中注册该类型及其依赖项,并让它为您解析一个实例。这样它会负责通过构造函数注入为您注入所有依赖项。

您应该在配置 OWIN 管道的 Startup.cs 中得到类似这样的模式。

public void Configuration(IAppBuilder app)
{
    var container = CreateContainerWithRegisteredServices();
    var dashboardFilter = container.Resolve<IDashboardAuthorizationFilter>();

    app.UseHangfireDashboard("/hangfire", new DashboardOptions { 
        Authorization = new [] { dashboardFilter }
    });

    BackgroundJob.Enqueue(() => Console.WriteLine("Hello world from Hangfire!"));

    // Configure Web API, SignalR, or whatever else hangs off your OWIN pipeline.
    // You can pass the container into their configure methods if necessary.   
}

IContainer CreateContainerWithRegisteredServices()
{
    //this method will look different depending on your chosen IoC library
    var container = SomeIoCLibrary.CreateContainer();
    container.Register<MyAuthorizationFilter, IDashboardAuthorizationFilter>();
    //register other dependencies here

    return container;
}