gio735
gio735
Explore posts from servers
CC#
Created by gio735 on 9/18/2024 in #help
EF-Core unable to create migration
No description
15 replies
CC#
Created by gio735 on 7/13/2024 in #help
✅ AutoValidateForgeryToken with fetch API
Hello, how can I pass the forgery token to the fetch API? I have tried putting it into the header as X-CSRF-TOKEN which seemingly creates token cookie, but it still can't reach specific Action
3 replies
CC#
Created by gio735 on 4/30/2024 in #help
Unable to get localization to get to work in MVC .net8
I'm trying to implement localization using cookies, but for some reason no matter what .AspNetCore.Culture equals it always uses default resx file value of cookie is c%3Dka-GE%7Cuic%3Dka-GE or c%3Den-US%7Cuic%3Den-US Program.cs
builder.Services.AddLocalization();
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[] { "en-US", "ka-GE" };
options.SetDefaultCulture(supportedCultures[0])
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
var defaultCookieRequestProvider =
options.RequestCultureProviders.FirstOrDefault(rcp =>
rcp.GetType() == typeof(CookieRequestCultureProvider));
if (defaultCookieRequestProvider != null)
options.RequestCultureProviders.Remove(defaultCookieRequestProvider);

options.RequestCultureProviders = new List<IRequestCultureProvider>()
{
new CookieRequestCultureProvider()
};
});
builder.Services.AddLocalization();
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[] { "en-US", "ka-GE" };
options.SetDefaultCulture(supportedCultures[0])
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
var defaultCookieRequestProvider =
options.RequestCultureProviders.FirstOrDefault(rcp =>
rcp.GetType() == typeof(CookieRequestCultureProvider));
if (defaultCookieRequestProvider != null)
options.RequestCultureProviders.Remove(defaultCookieRequestProvider);

options.RequestCultureProviders = new List<IRequestCultureProvider>()
{
new CookieRequestCultureProvider()
};
});
8 replies
CC#
Created by gio735 on 4/16/2024 in #help
✅ read-only builder.Services
public static IServiceCollection AddTokenAuthentication(this IServiceCollection services, string key)
{
var keybytes = Encoding.ASCII.GetBytes(key);

services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
x.TokenValidationParameters = new TokenValidationParameters
{
IssuerSigningKey = new SymmetricSecurityKey(keybytes),
ValidateIssuer = true,
ValidateAudience = true,
ValidIssuer = "localhost",
ValidAudience = "localhost"
});

return services;
}
public static IServiceCollection AddTokenAuthentication(this IServiceCollection services, string key)
{
var keybytes = Encoding.ASCII.GetBytes(key);

services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
x.TokenValidationParameters = new TokenValidationParameters
{
IssuerSigningKey = new SymmetricSecurityKey(keybytes),
ValidateIssuer = true,
ValidateAudience = true,
ValidIssuer = "localhost",
ValidAudience = "localhost"
});

return services;
}
services.AddAuthentication throws exception:
System.InvalidOperationException: 'The service collection cannot be modified because it is read-only.'
System.InvalidOperationException: 'The service collection cannot be modified because it is read-only.'
this exact approach works for API, in MVC project I'm planning to inject token into header through middleware.
2 replies
CC#
Created by gio735 on 3/26/2023 in #help
✅ for some reason API is not running, it has no exceptions at all, just reaches `app.Run()` and stop
3 replies
CC#
Created by gio735 on 3/26/2023 in #help
✅ asp.net api unable to find .xml file at startup likely problem of swagger
4 replies
CC#
Created by gio735 on 3/25/2023 in #help
✅ serilog and appsettings.json unable to build configuration
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File(
"../logs/webapi-Information-.log",
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information)
.WriteTo.File(
"../logs/webapi-Error-.log",
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Error)
.CreateLogger();
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File(
"../logs/webapi-Information-.log",
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information)
.WriteTo.File(
"../logs/webapi-Error-.log",
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Error)
.CreateLogger();
I am trying to write this in appsettings did it as such:
"Serilog": {
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.File"
],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.AspNetCore": "Warning",
"System": "Error"
}
},
"WriteTo:Information": [
{
"Name": "File",
"Args": {
"path": "../logs/webapi-Information-.log",
"rollingInterval": "Day",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
],
"WriteTo:Error": [
{
"Name": "File",
"Args": {
"path": "../logs/webapi-Error-.log",
"rollingInterval": "Day",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
],
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
]
}
"Serilog": {
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.File"
],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.AspNetCore": "Warning",
"System": "Error"
}
},
"WriteTo:Information": [
{
"Name": "File",
"Args": {
"path": "../logs/webapi-Information-.log",
"rollingInterval": "Day",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
],
"WriteTo:Error": [
{
"Name": "File",
"Args": {
"path": "../logs/webapi-Error-.log",
"rollingInterval": "Day",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
],
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
]
}
It shows exception that WriteTo:Error:Name has no Name element
6 replies
CC#
Created by gio735 on 3/19/2023 in #help
✅ IServiceCollection.Confgure<> not working
11 replies
CC#
Created by gio735 on 1/30/2023 in #help
❔ EF core - unable to map collection to database
13 replies
CC#
Created by gio735 on 10/27/2022 in #help
Unable to build due to foreign keys [Answered]
5 replies
CC#
Created by gio735 on 9/9/2022 in #help
System.Diagnostics.EventLog [Answered]
For some reason it's not appearing either with name or full name, I'm using .NET6 console app
11 replies