在 ASP.NET 5(或扩展 HttpContext)上不使用 DI 将对象附加到管道
Attach an object to the pipeline without DI on ASP.NET 5 (or extending HttpContext)
tl;博士
.- 有没有办法在自定义 class 的 HttpContext 中包含 属性(扩展),就像有一个 User 属性 一样,它是一个 ClaimsPrincipal?我想沿着所有管道(没有 DI)访问自定义 class 的 HttpContext.MyOwnProperty。
详细解释:
我已经在 ASP.NET 5 上创建了自己的身份系统,以摆脱框架的无数个不断变化的身份系统,我真的厌倦了它,并摆脱了使用索赔,这可能是非常标准化的,但在您遇到很多索赔时效率也很低。
我使用类型化对象来表示用户访问,并从内存(或共享缓存)存储库中获取它,该存储库是在管道早期注册的服务,以某种方式作为当前 UserManager。然后我将这个对象传递给管道,方法是将它的属性映射到我之前注册的范围服务:
services.AddSingleton<AccessManager>();
services.AddScoped<CurrentAccess>();
一切正常,但我想知道是否有更有效的方法将 CurrentAccess 对象附加到管道,而不是必须在每个地方注入和解析它,因为它在应用程序中被广泛使用.
目标是扩展 HttpContext class 以添加 CurrentAccess 类型的 属性,就像有一个 User 属性 一样ClaimsPrincipal. 那太好了,但我不知道该怎么做。有什么想法吗?
(我还尝试了另一种使用 HttpContext.Items 属性 的方法,这是一个 Dictionary
继续以与现在相同的方式注册您的服务。
services.AddSingleton<AccessManager>();
services.AddScoped<CurrentAccess>();
然后创建一个 HttpContext.GetCurrentAccess
扩展方法 returns CurrentAccess
对象:
public static class HttpContextExtensions
{
public static CurrentAccess GetCurrentAccess(this HttpContext httpContext)
{
return httpContext.RequestServices.GetRequiredService<CurrentAccess>();
}
}
这样,您可以在不想在构造函数中注入 CurrentAccess
依赖项的地方使用扩展方法作为便捷方法。
public IActionResult Index()
{
var currentAccess = this.Context.GetCurrentAccess();
...
return View();
}
在有意义的地方取依赖,在其他地方使用方便的扩展方法。
Is there a way to include a property (extend) on the HttpContext of a custom class, the same as there's a User property wich is a ClaimsPrincipal? I want to access HttpContext.MyOwnProperty of a custom class along all the pipeline (without DI).
您不能向 class 添加属性。不过,那将是一个很棒的 C# 功能 :)。
tl;博士 .- 有没有办法在自定义 class 的 HttpContext 中包含 属性(扩展),就像有一个 User 属性 一样,它是一个 ClaimsPrincipal?我想沿着所有管道(没有 DI)访问自定义 class 的 HttpContext.MyOwnProperty。
详细解释:
我已经在 ASP.NET 5 上创建了自己的身份系统,以摆脱框架的无数个不断变化的身份系统,我真的厌倦了它,并摆脱了使用索赔,这可能是非常标准化的,但在您遇到很多索赔时效率也很低。
我使用类型化对象来表示用户访问,并从内存(或共享缓存)存储库中获取它,该存储库是在管道早期注册的服务,以某种方式作为当前 UserManager。然后我将这个对象传递给管道,方法是将它的属性映射到我之前注册的范围服务:
services.AddSingleton<AccessManager>();
services.AddScoped<CurrentAccess>();
一切正常,但我想知道是否有更有效的方法将 CurrentAccess 对象附加到管道,而不是必须在每个地方注入和解析它,因为它在应用程序中被广泛使用.
目标是扩展 HttpContext class 以添加 CurrentAccess 类型的 属性,就像有一个 User 属性 一样ClaimsPrincipal. 那太好了,但我不知道该怎么做。有什么想法吗?
(我还尝试了另一种使用 HttpContext.Items 属性 的方法,这是一个 Dictionary
继续以与现在相同的方式注册您的服务。
services.AddSingleton<AccessManager>();
services.AddScoped<CurrentAccess>();
然后创建一个 HttpContext.GetCurrentAccess
扩展方法 returns CurrentAccess
对象:
public static class HttpContextExtensions
{
public static CurrentAccess GetCurrentAccess(this HttpContext httpContext)
{
return httpContext.RequestServices.GetRequiredService<CurrentAccess>();
}
}
这样,您可以在不想在构造函数中注入 CurrentAccess
依赖项的地方使用扩展方法作为便捷方法。
public IActionResult Index()
{
var currentAccess = this.Context.GetCurrentAccess();
...
return View();
}
在有意义的地方取依赖,在其他地方使用方便的扩展方法。
Is there a way to include a property (extend) on the HttpContext of a custom class, the same as there's a User property wich is a ClaimsPrincipal? I want to access HttpContext.MyOwnProperty of a custom class along all the pipeline (without DI).
您不能向 class 添加属性。不过,那将是一个很棒的 C# 功能 :)。