.net core 获取Jwt身份信息

       private readonly IOrder _order;
        private readonly ILog _log;
        private readonly IHttpContextAccessor _httpContextAccessor;
        public OrderController(IOrder order , ILog log , IHttpContextAccessor httpContextAccessor) 
        {
            _order=order;
            _log=log;
            _httpContextAccessor=httpContextAccessor;
        }

        [HttpPost("AddOrderInfosAsync")]
        public async Task<IActionResult> AddOrderInfosAsync([FromBody] OrderInfoDto orderInfoDto)
        {
            try
            {
            需要做c#.net 项目的,有时间并且想赚零花钱的老哥,请加Q群:741058172// 获取账号信息的声明   
                var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
                await _order.AddOrderInfo(orderInfoDto,accountNumberClaim);
                await _order.SaveAsync();
            }
            catch 
            {
                return BadRequest("添加异常,请查看日志。");
            }
            return Ok();
        }

记得在控制器加入: [Authorize(AuthenticationSchemes = "Bearer")]特性
另外记得在Startup.csConfigureServices中注入以下服务

   services.AddHttpContextAccessor();

总是忘记怎么获取,记录下。

你可能感兴趣的:(.netcore)