baristaner
baristaner
CC#
Created by baristaner on 1/11/2024 in #help
How can i use ASP.NET CORE Identity with JWT
Hello guys i'm using Identity Package for authentication,and i want to implement JWT TOKEN to my auth system Right now endpoints that comes with Identity works fine it returns token but it doesnt look like a JWT Token The Login EP Returns
{
CfDJ8ALPmkxk6X1NoFcQPGKzyblX6sAbwACCXF3V2byfWzg7QhfSa47_VEp2AIhtUk9sY9ylJuQZowEkHYBydFCW-cbAMgnRmcv2BdH8bRTRNsgLkBskszwj-G4OOqz31CrfkQLKNDAVW497BnL240v8IGxTxUxw5tsn0X7bbuJmcq7jC4K6G36eBBSVk7WtaGz3C3ZqgkTCgoyTAX1QrL8dl7t3yW6tZqczdwPTWyC7KYWQPqqGXwZAFy8VyL1gsv_P2_kRN8G9OowsyKuB4Z4hJ3M6ZWQBxMPy2AH-j91i6CCuKIjcD6GH3QR82ZDN-Ly-H-cJwB721dUOR0lrNVXijaoGEYPPj0YcbGHxn3sUwAfZlT9n3-qVhGs65HlA8KtkQa-uo6deKsP4ENJOopnfT8g76JovsDSx0QNXhlgQBNX9aoNDrCU6BJmJlFhm5THMxbTyeKT6uQXQgN811B4SivmeIIwNZS9sf9S8hBYAHFDiExECIZo6g8PBjWUzoJ2eiEozuazFku57twICxIUZT3JMWolozK3Ms1v76tzU-xa120geZ9A5qXyBu_fr_RcycMan1JN0w2-1xcR2w4b7x_BagSagc9N3QyutXWugBogBiHQC5j9nOFgF983Fgd426SgSk_aW_59538u5uhmWmpiwsIki9H0O5AmsIR_glQvnEoeaEY216RQuuDGcm-WkqA,
}
{
CfDJ8ALPmkxk6X1NoFcQPGKzyblX6sAbwACCXF3V2byfWzg7QhfSa47_VEp2AIhtUk9sY9ylJuQZowEkHYBydFCW-cbAMgnRmcv2BdH8bRTRNsgLkBskszwj-G4OOqz31CrfkQLKNDAVW497BnL240v8IGxTxUxw5tsn0X7bbuJmcq7jC4K6G36eBBSVk7WtaGz3C3ZqgkTCgoyTAX1QrL8dl7t3yW6tZqczdwPTWyC7KYWQPqqGXwZAFy8VyL1gsv_P2_kRN8G9OowsyKuB4Z4hJ3M6ZWQBxMPy2AH-j91i6CCuKIjcD6GH3QR82ZDN-Ly-H-cJwB721dUOR0lrNVXijaoGEYPPj0YcbGHxn3sUwAfZlT9n3-qVhGs65HlA8KtkQa-uo6deKsP4ENJOopnfT8g76JovsDSx0QNXhlgQBNX9aoNDrCU6BJmJlFhm5THMxbTyeKT6uQXQgN811B4SivmeIIwNZS9sf9S8hBYAHFDiExECIZo6g8PBjWUzoJ2eiEozuazFku57twICxIUZT3JMWolozK3Ms1v76tzU-xa120geZ9A5qXyBu_fr_RcycMan1JN0w2-1xcR2w4b7x_BagSagc9N3QyutXWugBogBiHQC5j9nOFgF983Fgd426SgSk_aW_59538u5uhmWmpiwsIki9H0O5AmsIR_glQvnEoeaEY216RQuuDGcm-WkqA,
}
And here is my AddAuthentication in Program cs
c#
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("JWT_SECRET")),

ValidateIssuer = true,
ValidIssuer = "https://localhost:5000/", // Your identity provider URL

ValidateAudience = true,
ValidAudience = "https://localhost:5001/", // Your resource/API URL

ValidateLifetime = true
};
});
c#
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("JWT_SECRET")),

ValidateIssuer = true,
ValidIssuer = "https://localhost:5000/", // Your identity provider URL

ValidateAudience = true,
ValidAudience = "https://localhost:5001/", // Your resource/API URL

ValidateLifetime = true
};
});
5 replies
CC#
Created by baristaner on 1/4/2024 in #help
Implementing MediatR
How can i implement MediatR library to redirect the request relevant Service class (like GenerateCodeService, CreateProductService) And i don't wanna use CQRS pattern
76 replies
CC#
Created by baristaner on 12/27/2023 in #help
DateTime.UtcNow returns 0001-01-01T00:00:00
Hello guys I have BaseEntity and it has createdAt,modifiedAt values in it So the problem is when i run this code it returns
"createdAt": "2023-12-27T13:57:08.3933333",
"modifiedAt": "0001-01-01T00:00:00"
"createdAt": "2023-12-27T13:57:08.3933333",
"modifiedAt": "0001-01-01T00:00:00"
But i want the values to be same when it's first created
c#
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
DateTime now = DateTime.UtcNow;

foreach (var entry in ChangeTracker.Entries<BaseEntity>())
{
switch (entry.State)
{
case EntityState.Added:
if (entry.Entity.CreatedAt == DateTime.MinValue)
{
entry.Entity.CreatedAt = now;
}
entry.Entity.ModifiedAt = now;
break;

case EntityState.Modified:
entry.Entity.ModifiedAt = now;
break;
}
Console.WriteLine($"Entity Type: {entry.Entity.GetType().Name}, State: {entry.State}, CreatedAt: {entry.Entity.CreatedAt}, ModifiedAt: {entry.Entity.ModifiedAt}");
}

return await base.SaveChangesAsync(cancellationToken);
}
}
c#
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
DateTime now = DateTime.UtcNow;

foreach (var entry in ChangeTracker.Entries<BaseEntity>())
{
switch (entry.State)
{
case EntityState.Added:
if (entry.Entity.CreatedAt == DateTime.MinValue)
{
entry.Entity.CreatedAt = now;
}
entry.Entity.ModifiedAt = now;
break;

case EntityState.Modified:
entry.Entity.ModifiedAt = now;
break;
}
Console.WriteLine($"Entity Type: {entry.Entity.GetType().Name}, State: {entry.State}, CreatedAt: {entry.Entity.CreatedAt}, ModifiedAt: {entry.Entity.ModifiedAt}");
}

return await base.SaveChangesAsync(cancellationToken);
}
}
51 replies