有没有一种方法可以实现下一个行为?HttpContextAccessor,IPrincipal和ServiceCollection
public static void Configure(IServiceCollection services) {
services.AddScoped(typeof(Func<IPrincipal>), ???);
services.AddInstance(typeof(Func<IPrincipal>), ???);
}
1.不起作用:
Func<IServiceProvider, IPrincipal> getPrincipal =
(sp) => sp.GetService<IHttpContextAccessor>().HttpContext.User;
services.AddScoped(
typeof(Func<IPrincipal>),
getPrincipal);
2.不起作用:
var builder = services.BuildServiceProvider();
services.AddInstance(
typeof(Func<IPrincipal>),
builder.GetService<IHttpContextAccessor>().HttpContext.User);
什么是你想达到什么目的?目前还不清楚你需要什么样的最终结果。例如,你想有一个服务返回当前用户? –