我有一个UserContext Service,其中将放置一些基本功能("IsAuthenticated,GetUser等...)
I have a UserContext Service where I'll put some basic functionalities ("IsAuthenticated, GetUser etc...)
为此,我需要将HTTPContext从我的WebAPI控制器传递到我的类库服务.
In order to do that, I need to pass the HTTPContext from my WebAPI Controller to my Class Library Service.
实际上,在Web api控制器中HttpContext始终是null.
Actually, HttpContext is always null in the web api controller.
有人能解决我的问题吗?. 有没有更好的方法来实现它.
Anybody have a solution to resolve my issue ?. Is there a better way to acheive it.
Web API用户控制器
[Route("api/[controller]")] [Authorize] public class UserController : Controller { private readonly IUserContextServices _userContextServices; private readonly User loggedUser; public UserController() { //HttpContext ALWAYS NULL _userContextServices = new UserContextService(HttpContext); } }UserContext Services
namespace MyProj.Services { public interface IUserContextServices { UserContext GetUserContext(); bool IsUserAuthenticated(); } public class UserContextService : IUserContextServices { private readonly HttpContext _context; private UserContext _userContext; public UserContextService(HttpContext context) { _context = context; InitUserContext(); } private IEnumerable<Claim> GetUserClaims() { if (IsUserAuthenticated()) { return _context.User.Claims; } return null; } private void InitUserContext() { if (IsUserAuthenticated()) { var claims = GetUserClaims(); _userContext = new UserContext(); _userContext.Email = claims.First(p => p.Type == "email").Value; _userContext.AspNetUserID = claims.First(p => p.Type == "sub").Value; } } public UserContext GetUserContext() { return _userContext; } public bool IsUserAuthenticated() { return _context.User != null && _context.User.Identity != null && _context.User.Identity.IsAuthenticated; } } }推荐答案
HttpContext在调用Controller的构造函数时不可用.您将不得不重新设计代码,以便稍后在调用流程中获取上下文.这就是 IHttpContextAccessor 是用于.
HttpContext is not available when the constructor of the Controller is called. You are going to have to redesign your code to get the context later in the invocation flow. This is what the IHttpContextAccessor is for.
public interface IHttpContextAccessor { HttpContext HttpContext { get; } }将其注入服务中,然后根据需要稍后访问上下文.
Inject that into the service and then access the context later as needed.
public class UserContextService : IUserContextServices { private readonly IHttpContextAccessor contextAccessor; private UserContext _userContext; public UserContextService(IHttpContextAccessor accessor) { contextAccessor = accessor; } private HttpContext Context { get { return contextAccessor.HttpContext; } } public UserContext GetUserContext() { if (_userContext == null && IsUserAuthenticated()) { var claims = Context?.User?.Claims; _userContext = new UserContext() { Email = claims.First(p => p.Type == "email").Value, AspNetUserID = claims.First(p => p.Type == "sub").Value }; } return _userContext; } public bool IsUserAuthenticated() { return Context?.User?.Identity?.IsAuthenticated; } }将服务抽象注入Controller
Inject the service abstraction into the Controller
[Route("api/[controller]")] [Authorize] public class UserController : Controller { private readonly IUserContextServices _userContextServices; private readonly User loggedUser; public UserController(IUserContextServices userContextServices) { _userContextServices = userContextServices; } //... }默认情况下,
IHttpContextAccessor不在服务集合中,因此您需要手动将其添加到Startup.ConfigureServices中以便能够将其注入:
IHttpContextAccessor is not in the service collection by default so you need to add it in Startup.ConfigureServices manually in order to be able to inject it:
services.AddHttpContextAccessor(); services.AddTransient<IUserContextServices, UserContextService>();更多推荐
HttpContext在构造函数中为null
发布评论