2

我有一个使用 Topshelf 在 Windows 服务中托管的 Hangfire 实例:

static void Main()
{
    HostFactory.Run(x =>
    {
        x.Service<Application>(s =>
        {
            s.ConstructUsing(name => new Application());
            s.WhenStarted(tc => tc.Start());
            s.WhenStopped(tc => tc.Stop());
        });
        x.RunAsLocalSystem();

        x.SetDescription("Hangfire Windows Service Sample");
        x.SetDisplayName("Hangfire Windows Service Sample");
        x.SetServiceName("hangfire-sample");
    });
}

private class Application
{
    private IDisposable host;

    public void Start()
    {
        host = WebApp.Start<Startup>("http://localhost:12345");

        Console.WriteLine();
        Console.WriteLine("Hangfire Server started.");
        Console.WriteLine("Dashboard is available at {0}/hangfire", configSettings.Jobs.EndPoint);
        Console.WriteLine();
    }

    public void Stop()
    {
        host.Dispose();
    }
}

我的 StartUp 课程非常基础:

public void Configuration(IAppBuilder app)
{
    GlobalConfiguration.Configuration.UseSqlServerStorage(
        "DefaultConnection",
        new SqlServerStorageOptions {QueuePollInterval = TimeSpan.FromMinutes(1)});    

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

    app.UseHangfireServer();
}

我正在尝试使用自定义授权过滤器:

public class AuthorizationFilter : IAuthorizationFilter
{
    public bool Authorize(IDictionary<string, object> owinEnvironment)
    {
        var context = new OwinContext(owinEnvironment);

        return context.Authentication.User.Identity.IsAuthenticated;
    }
}

我希望用来context.Authentication.User进行身份验证,但它总是返回 null。

有什么方法可以使这项工作适用于自托管的hangfire服务?

4

0 回答 0