我突然遇到未经授权的/内部服务器错误

时间:2019-05-23 01:02:43

标签: angular jwt asp.net-core-2.0

我正在使用.NET作为后端在Angular中创建此网页。一切正常,但是今天(突然)我收到了未经授权的错误(401)。删除授权后,我将收到内部服务器错误(表示我的“ User.Identity.Name”)不起作用。所以我猜我的JWT有问题

我尝试删除授权标头。但这给了我一个内部服务器错误(500)。唯一有效的请求是那些不使用IdentityUser的请求,因此为[AllowAnonymous]

我不知道可能是什么问题,因此很难显示一些代码。但由于我认为问题与我的JWT有关,因此我将添加一些代码。

    [Route("api/[controller]")]
        [ApiController]
        [Produces("application/json")]
        [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
        public class BierenController : ControllerBase
        {
            private readonly IBierRepository _bierRepository;
            private readonly IGebruikerRepository _gebruikerRepository;
            private readonly IBrouwerRepository _brouwerRepository;

            public BierenController(IBierRepository bierRepository, IGebruikerRepository gebruikerRepository, IBrouwerRepository brouwerRepository)
            {
                _bierRepository = bierRepository;
                _gebruikerRepository = gebruikerRepository;
                _brouwerRepository = brouwerRepository;
            }


            [HttpGet]
            [AllowAnonymous]
            public IEnumerable<Bier> GetBieren()
            {
                return _bierRepository.GetAll().OrderBy(b => b.Naam);
            }

            [HttpGet("{id}")]
            [AllowAnonymous]
            public ActionResult<Bier> GetBier(int id)
            {
                Bier bier = _bierRepository.GetBy(id);
                if (bier == null)
                    return NotFound();
                return bier;
            }

            [HttpPost]
            [Authorize(Roles = "Brouwer, Admin")]
            public IActionResult PostBier(Bier bier)
            {
                Brouwer brouwer = _brouwerRepository.GetBy(User.Identity.Name);
                brouwer.AddBier(bier);
                _brouwerRepository.SaveChanges();
                return NoContent();
            }

感谢任何帮助,如果有人可以帮助,看到一些我很乐意提供的额外代码。 Screenshot of error

0 个答案:

没有答案