HttpContext在构造函数中为null

编程入门 行业动态 更新时间:2024-10-27 03:26:29
本文介绍了HttpContext在构造函数中为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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

本文发布于:2023-11-16 09:46:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603164.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中为   函数   HttpContext   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!