仪表板授权(自托管为Windows服务)

时间:2015-05-06 23:07:43

标签: c# owin hangfire

我使用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服务?

0 个答案:

没有答案