DarkVader
DarkVader
CC#
Created by DarkVader on 12/12/2023 in #help
Encoding.Default.GetString() and then Encoding.Default.GetBytes() create two different byte[], How?
No description
2 replies
CC#
Created by DarkVader on 6/4/2023 in #help
✅ .Where in Include stetment not returning correctly
queryable
.Include(x => x.Comments
.Where(x => x.NestedLevel == 0 && x.ParentId == null)
.OrderByDescending(x => x.CreatedAtUtc)
.Skip(0)
.Take(5)
).ThenInclude(x => x.Comments
.Where(x => x.NestedLevel == 1)
.OrderByDescending(x => x.CreatedAtUtc)
.Take(3)
);
queryable
.Include(x => x.Comments
.Where(x => x.NestedLevel == 0 && x.ParentId == null)
.OrderByDescending(x => x.CreatedAtUtc)
.Skip(0)
.Take(5)
).ThenInclude(x => x.Comments
.Where(x => x.NestedLevel == 1)
.OrderByDescending(x => x.CreatedAtUtc)
.Take(3)
);
I dont understand why does this pull from database like this, shouldnt it be 5 Comments on first level (with NestedLevel == 0) and then inside of them list with comments with (NestedLevel == 1) like a tree , for some reason this pulls 8 comments in the root level list, where not all comments are NestedLevel = 0, I am so confused, I am reading documentation and it shouldnt behave like this.
9 replies
CC#
Created by DarkVader on 6/3/2023 in #help
✅ Remove many to many with select
So here is the code snippet,
var post = await _ctx.Posts
.Include(x => x.LikedBy)
.Include(x => x.DislikedBy)
.Include(x => x.SeenBy)
.Select(x => new PostDo
{
Id = x.Id,
IsLiked = x.LikedBy.Any(x => x.Id == _userId),
IsDisliked = x.DislikedBy.Any(x => x.Id == _userId),
IsSeen = x.SeenBy.Any(x => x.Id == _userId),
LikedBy = x.LikedBy.Where(x => x.Id == _userId).ToList(),
DislikedBy = x.DislikedBy.Where(x => x.Id == _userId).ToList(),
SeenBy = x.SeenBy.Where(x => x.Id == _userId).ToList(),
})
.FirstOrDefaultAsync(x => x.Id == id, cancellationToken);
post.DisliekdBy.Remove(user);
_ctx.Posts.Update(post); // This is not updating property DislikedBy
await _ctx.SaveChangesAsync(cancellationToken) > 0;
var post = await _ctx.Posts
.Include(x => x.LikedBy)
.Include(x => x.DislikedBy)
.Include(x => x.SeenBy)
.Select(x => new PostDo
{
Id = x.Id,
IsLiked = x.LikedBy.Any(x => x.Id == _userId),
IsDisliked = x.DislikedBy.Any(x => x.Id == _userId),
IsSeen = x.SeenBy.Any(x => x.Id == _userId),
LikedBy = x.LikedBy.Where(x => x.Id == _userId).ToList(),
DislikedBy = x.DislikedBy.Where(x => x.Id == _userId).ToList(),
SeenBy = x.SeenBy.Where(x => x.Id == _userId).ToList(),
})
.FirstOrDefaultAsync(x => x.Id == id, cancellationToken);
post.DisliekdBy.Remove(user);
_ctx.Posts.Update(post); // This is not updating property DislikedBy
await _ctx.SaveChangesAsync(cancellationToken) > 0;
If I add something to any of the Lists LikedBy, DislikedBy and SeenBy they are added, but if I remove, they are not removed I read the docs and I know that LikedBy, DislikedBy and SeenBy are not tracked, but I honestly cant find any smart way of doing this except pulling all of the LikedBy, DislikedBy and SeenBy but that is memory inefficient and I have multiple DB calls, so is there something like this only for lists
using var context = new BlogsContext();
var blog = context.Blogs.Include(e => e.Posts).First(e => e.Name == ".NET Blog");

// Change a property value
context.Entry(blog).Property(e => e.Name).CurrentValue = ".NET Blog (Updated!)";

// Add a new entity to the DbContext
context.Add(
new Post
{
Blog = blog,
Title = "What’s next for System.Text.Json?",
Content = ".NET 5.0 was released recently and has come with many..."
});

Console.WriteLine(context.ChangeTracker.DebugView.LongView);
using var context = new BlogsContext();
var blog = context.Blogs.Include(e => e.Posts).First(e => e.Name == ".NET Blog");

// Change a property value
context.Entry(blog).Property(e => e.Name).CurrentValue = ".NET Blog (Updated!)";

// Add a new entity to the DbContext
context.Add(
new Post
{
Blog = blog,
Title = "What’s next for System.Text.Json?",
Content = ".NET 5.0 was released recently and has come with many..."
});

Console.WriteLine(context.ChangeTracker.DebugView.LongView);
5 replies
CC#
Created by DarkVader on 6/3/2023 in #help
ClaimsPrincipal, httpContext.User is not filled when I send the token, but endpoint is AllowAnonymos
public static Guid? GetUserId(this ClaimsPrincipal user)
{
var userId = user.Claims.FirstOrDefault(c => c.Type == ClaimEnum.Id.ToString());
return userId is null ? null : Guid.Parse(userId.Value);
}
public static Guid? GetUserId(this ClaimsPrincipal user)
{
var userId = user.Claims.FirstOrDefault(c => c.Type == ClaimEnum.Id.ToString());
return userId is null ? null : Guid.Parse(userId.Value);
}
So I am using minimal Apis and my endpoint looks like this
app.MapGet(BasePath + "/{id:guid}", GetPost)
.AllowAnonymous();
app.MapGet(BasePath + "/{id:guid}", GetPost)
.AllowAnonymous();
When I want to use this function on httpContext.User I always get null, even if I sent the token trough the header
curl -X 'GET' \
'https://localhost:7067/posts/994f3e31-155a-42cb-983c-c13478951235' \
-H 'accept: */*' \
-H 'Authorization: Bearer {Token}'
curl -X 'GET' \
'https://localhost:7067/posts/994f3e31-155a-42cb-983c-c13478951235' \
-H 'accept: */*' \
-H 'Authorization: Bearer {Token}'
I have CurrentUserService which I use and it looks like this
public class CurrentUserService : ICurrentUserService
{
private readonly IHttpContextAccessor _httpContextAccessor;

public CurrentUserService(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;

// This is the part where I wanted to read token if it is null but it has Authorization header, but I couldnt read claims on the .GetUserId() function
if (_httpContextAccessor.HttpContext?.User.GetUserId() is null &&
!string.IsNullOrWhiteSpace(_httpContextAccessor.HttpContext?.Request.Headers.Authorization.ToString()))
{
var token = new JwtSecurityTokenHandler().ReadJwtToken(_httpContextAccessor.HttpContext?.Request.Headers.Authorization.ToString().Split(' ')[1]);

_httpContextAccessor.HttpContext!.User = new ClaimsPrincipal(new ClaimsIdentity(token.Claims));
}
}

public Guid? UserId => _httpContextAccessor.HttpContext?.User.GetUserId();

public string? Role => _httpContextAccessor.HttpContext?.User?.GetRole();
}
public class CurrentUserService : ICurrentUserService
{
private readonly IHttpContextAccessor _httpContextAccessor;

public CurrentUserService(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;

// This is the part where I wanted to read token if it is null but it has Authorization header, but I couldnt read claims on the .GetUserId() function
if (_httpContextAccessor.HttpContext?.User.GetUserId() is null &&
!string.IsNullOrWhiteSpace(_httpContextAccessor.HttpContext?.Request.Headers.Authorization.ToString()))
{
var token = new JwtSecurityTokenHandler().ReadJwtToken(_httpContextAccessor.HttpContext?.Request.Headers.Authorization.ToString().Split(' ')[1]);

_httpContextAccessor.HttpContext!.User = new ClaimsPrincipal(new ClaimsIdentity(token.Claims));
}
}

public Guid? UserId => _httpContextAccessor.HttpContext?.User.GetUserId();

public string? Role => _httpContextAccessor.HttpContext?.User?.GetRole();
}
6 replies