Asp.net 更复杂的Hangfire仪表板身份验证
我正在应用程序上使用hangfire,我需要使用我的存储库进行仪表板验证。为此,我必须在Hangfire的Authorize方法中解析存储库,但是,使用Asp.net 更复杂的Hangfire仪表板身份验证,asp.net,hangfire,Asp.net,Hangfire,我正在应用程序上使用hangfire,我需要使用我的存储库进行仪表板验证。为此,我必须在Hangfire的Authorize方法中解析存储库,但是,使用OwinContext我无法这样做。我选择在这个项目中使用SimpleInjector,因为它在WebApiConfigsRegister方法中注册了所有内容,所以我想访问它。最近,我使用了中间件作为MessageHandler,从中,我使用HttpRequestMessage成功地解决了一个依赖关系。但是在OwinContext上,我无法使用H
OwinContext
我无法这样做。我选择在这个项目中使用SimpleInjector,因为它在WebApiConfig
sRegister
方法中注册了所有内容,所以我想访问它。最近,我使用了中间件作为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.cs
sConfiguration
上说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!"));
}
private IEnumerable GetHangfireServers()
{
Hangfire.GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(“CONN_STR”),新的SqlServerStorageOptions
{
CommandBatchMaxTimeout=TimeSpan.FromMinutes(5),
SlidingVisibilityTimeout=时间跨度从分钟(5),
QueuePollInterval=TimeSpan.Zero,
UseRecommendedIsolationLevel=true,
DisableGloballock=true
});
返回新的BackgroundJobServer();
}
公共无效配置(IAppBuilder应用程序)
{
应用程序UseHangfireAspNet(GetHangfireServers);
app.UseHangfireDashboard(“/hangfire”),新的仪表板选项{
授权=新[]{
新仪表板授权()
/*解释*/
//让DI通过构造函数工作,
//我必须在这里将AuthRepository作为参数。
//我的AuthRepository也有很多DIs所以,
//通过这里是不可能的。
}
});
BackgroundJob.Enqueue(()=>Console.WriteLine(“来自Hangfire的你好世界”);
}
顺便说一下,我的项目是一个.Net Framework 4.7.2项目。就Hangfire而言,您负责提供实现IDashboardAuthorizationFilter的类的实例。所以,如果该类具有您想要注入的依赖项,那么这将由您来连接。最好在DI容器中注册该类型及其依赖项,并让它为您解析一个实例。这样,它就可以通过构造函数注入为您注入所有依赖项
在Startup.cs中配置OWIN管道时,您应该会得到类似这样的模式
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;
}
public void配置(IAppBuilder应用程序)
{
var container=CreateContainerWithRegisteredServices();
var dashboardFilter=container.Resolve();
app.UseHangfireDashboard(“/hangfire”),新的仪表板选项{
授权=新建[]{dashboardFilter}
});
BackgroundJob.Enqueue(()=>Console.WriteLine(“来自Hangfire的你好世界”);
//配置Web API、信号器或其他任何挂起OWIN管道的东西。
//如有必要,您可以将容器传递到其配置方法中。
}
IContainer CreateContainerWithRegisteredServices()
{
//根据您选择的IoC库,此方法看起来会有所不同
var container=SomeIoCLibrary.CreateContainer();
container.Register();
//在此处注册其他依赖项
返回容器;
}
您是否尝试过构造函数注入?它起作用了吗?结果是什么?不,为了做到这一点,我必须从Startup.cs
中给出结果,而我似乎无法做到。为什么?当你不能做某事时,请清楚是什么阻止了你。@mason我编辑了这个问题来说明原因,谢谢你的建议:)你不需要把它们放在一个文件中。在Startup.cs中,实例化DI容器并注册服务。然后,您可以将该容器传递到WebAPI配置、Hangfire配置以及任何需要它的地方。