h
h
CC#
Created by h on 7/9/2023 in #help
✅ 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);
}
}
}
11 replies
CC#
Created by h on 7/3/2023 in #help
✅ ASP.NET Core Web API application crashes with no error message only in some environments
I am working on a Web API project which runs fine in Docker and on a Windows machine, but crashes with no error message and an exit code of 139 (segfault) on Linux. What troubleshooting steps should I take?
71 replies
CC#
Created by h on 3/3/2023 in #help
❔ repo doesnt connect to github on another pc
my repo (solution) is on a usb stick, i've created it on another pc and pushed it to github, now i am using it on my pc and it doesnt want to connect to said github repo how do i fix this and allow my local git project to be pushed to github
4 replies