© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
10 replies
h

✅ Endpoint returns 400 when I add authorization code, works correctly without

Here is the authorization code:
    public class UpdateUserAuthorizationHandler : AuthorizationHandler<UpdateUserRequirement>
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly UserService _userService;
        private readonly IMapper _mapper;

        public UpdateUserAuthorizationHandler(IHttpContextAccessor httpContextAccessor, UserService userService, IMapper mapper)
        {
            _httpContextAccessor = httpContextAccessor;
            _userService = userService;
            _mapper = mapper;
        }

        protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, UpdateUserRequirement requirement)
        {
            if (!context.User.Identity.IsAuthenticated)
            {
                throw new InvalidTokenException("access");
            }

            var userId = int.Parse(context.User.Claims.First(c => c.Type == "id").Value);

            var httpContext = _httpContextAccessor.HttpContext;
            var bodyData = string.Empty;
            using (var reader = new StreamReader(httpContext.Request.Body, Encoding.UTF8))
            {
                bodyData = await reader.ReadToEndAsync();
            }
            var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true
            };
            var userDto = JsonSerializer.Deserialize<UserDTO>(bodyData, options);
            var targetUser = await _userService.GetUserById(userDto.Id);

            if (targetUser != null && targetUser.Id == userId)
            {
                context.Succeed(requirement);
            }
            else
            {
                throw new UnauthorizedActionException(userId);
            }
        }
    }
    public class UpdateUserAuthorizationHandler : AuthorizationHandler<UpdateUserRequirement>
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly UserService _userService;
        private readonly IMapper _mapper;

        public UpdateUserAuthorizationHandler(IHttpContextAccessor httpContextAccessor, UserService userService, IMapper mapper)
        {
            _httpContextAccessor = httpContextAccessor;
            _userService = userService;
            _mapper = mapper;
        }

        protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, UpdateUserRequirement requirement)
        {
            if (!context.User.Identity.IsAuthenticated)
            {
                throw new InvalidTokenException("access");
            }

            var userId = int.Parse(context.User.Claims.First(c => c.Type == "id").Value);

            var httpContext = _httpContextAccessor.HttpContext;
            var bodyData = string.Empty;
            using (var reader = new StreamReader(httpContext.Request.Body, Encoding.UTF8))
            {
                bodyData = await reader.ReadToEndAsync();
            }
            var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true
            };
            var userDto = JsonSerializer.Deserialize<UserDTO>(bodyData, options);
            var targetUser = await _userService.GetUserById(userDto.Id);

            if (targetUser != null && targetUser.Id == userId)
            {
                context.Succeed(requirement);
            }
            else
            {
                throw new UnauthorizedActionException(userId);
            }
        }
    }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Authorised endpoint returns 401
C#CC# / help
3y ago
Testing my UserEdit Api endpoint using swagger fails and throws a code 400 response
C#CC# / help
2y ago
Receiving 400 on production, local env works fine
C#CC# / help
2y ago