【发布时间】:2019-11-30 18:09:55
【问题描述】:
我已经阅读了所有其他关于这个问题的帖子,我确定这不是重复的。
我正在构建一个 Razor 页面过滤器以在我的 Startup.cs 类中使用,我需要访问 HttpContext。 I would normally do this through constructor injection using ASP.NET Core's DI capabilities. 我什至在 Startup.cs 中有通常的 services.AddHttpContextAccessor() 语句,并在我的项目的其他地方使用它。
由于我正在构建一个派生自 IAsyncPageFilter 并从 Startup.cs 类创建的 Razor page filter,因此它看起来好像我不能注入它(因为它是由启动创建的,而不是注入的)。
这里是添加过滤器到Startup.cs:
Startup.cs(截图)
services.AddMvc(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
options.Filters.Add(new RazorAsyncPageFilter(_logger, Configuration)); <---my filter
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
这是我的过滤器:
RazorAsyncPageFilter.cs
public class RazorAsyncPageFilter : IAsyncPageFilter
{
private readonly ILogger _logger;
private readonly IConfiguration _configuration;
public string[] Scopes { get; set; }
public string ScopeKeySection { get; set; }
public RazorAsyncPageFilter(ILogger logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}
public async Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context)
{
await Task.CompletedTask;
}
public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
{
VaultGraphServiceClient.OnMsalUiExceptionEvent += VaultGraphServiceClientOnOnMsalUiExceptionEvent;
await next.Invoke();
}
private void VaultGraphServiceClientOnOnMsalUiExceptionEvent(object sender, MsalUiRequiredException e)
{
_logger.LogInformation($"Triggered authentication exception: {e.Message}");
Scopes = new string[] { _configuration.GetValue<string>(ScopeKeySection) };
var properties = BuildAuthenticationPropertiesForIncrementalConsent(Scopes, e, **I_NEED_CONTEXT_HERE**);
new ChallengeResult(properties);
}
}
注意从另一个类中的事件处理程序触发的方法调用所需的上下文。我想将HttpContext 传递给过滤器类中的私有方法。
这是我尝试过的:
-
在
Startup.cs中为IHttpContextAccessor _httpContext定义一个字段并将其传递给过滤器,但这显然不起作用,因为该上下文尚未建立。 -
从另一个方法传递上下文。这需要更多的工作,并且充满了warnings about passing it to a background thread。
-
从我的调用
EventHandler中注入上下文。不幸的是,这是从静态类调用的,据我所知,静态类中没有 DI 的概念。它是否正确?此外,我正在使用一个EventHandler委托来传递MsalUiRequiredException对象,并且只能传递一个对象。也许我可以为event类型使用不同的处理程序,但我对事件不够熟悉,无法理解如何做到这一点。 -
我正在考虑创建一个自定义类来保存
MsalUiRequiredException和HttpContext可以在其中注入/传递到其他地方,但这似乎也有点矫枉过正,或者可能是上面 #2 的问题。
有什么建议吗?
【问题讨论】:
-
我有一个想法,但不是答案。所以当我在 Asp.Net MVC 中做同样的事情时,我创建了一个新类并从 WebViewPage 抽象类继承。 F.e.:
abstract class TViewPage<TModel> : System.Web.Mvc.WebViewPage<TModel>您可以在 Asp.Net Core 中使用 RazorPage 类而不是 WebViewPage -
什么是
VaultGraphServiceClient.OnMsalUiExceptionEvent -
@Nkosi 它是对另一个静态类触发的事件的订阅。
标签: c# asp.net-core razor