Welles
Welles
Explore posts from servers
CC#
Created by Welles on 10/26/2022 in #help
Fastest way to change all items in a nested list
Hi there, regex was an option. The jsonstring will always be single line. However not sure there is much performance gain using regex over for loops. I just don’t like to see nested for loops, but in machine code for loops are actually pretty fast, so this might just be the best solution. Thanks!
8 replies
CC#
Created by Welles on 10/24/2022 in #help
azure functions isolated process
My serilog config is in local.settings.json, not sure if this is ok
2 replies
CC#
Created by Welles on 9/22/2022 in #help
.net core webapi2 API key authorization
could also serialize the claims stuff myself ofcourse, working with DTO now, works like a charm
44 replies
CC#
Created by Welles on 9/22/2022 in #help
.net core webapi2 API key authorization
I can work around it with DTO, but then the claims part would be rather useless as it isn't stored.
44 replies
CC#
Created by Welles on 9/22/2022 in #help
.net core webapi2 API key authorization
I don't really think it is possible to use it for database stuff tbh. the Iapikey expects a public IReadOnlyCollection<Claim> Claims { get; } Not sure how I would get that into the model and database
44 replies
CC#
Created by Welles on 9/22/2022 in #help
.net core webapi2 API key authorization
thanks!
44 replies
CC#
Created by Welles on 9/22/2022 in #help
.net core webapi2 API key authorization
Alright I'll check it to see if I can use it with dbcontext 🙂
44 replies
CC#
Created by Welles on 9/22/2022 in #help
.net core webapi2 API key authorization
All I can find on the internet is telling me to use middleware
44 replies
CC#
Created by Welles on 9/22/2022 in #help
.net core webapi2 API key authorization
so I got this
public sealed class ApiAuthenticationMiddleware {
private readonly RequestDelegate _next;
private const string _headerName = "x-api-key";

public ApiAuthenticationMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task InvokeAsync(HttpContext context, [FromServices] AuthenticationService authenticationService)
{
if (!context.Request.Headers.TryGetValue(_headerName, out
var extractedApiKey))
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Api Key was not provided ");
return;
}
var valid = authenticationService.IsApiKeyValid(extractedApiKey);
if (!valid)
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Unauthorized client");
return;
}
await _next(context);
}
}
public sealed class ApiAuthenticationMiddleware {
private readonly RequestDelegate _next;
private const string _headerName = "x-api-key";

public ApiAuthenticationMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task InvokeAsync(HttpContext context, [FromServices] AuthenticationService authenticationService)
{
if (!context.Request.Headers.TryGetValue(_headerName, out
var extractedApiKey))
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Api Key was not provided ");
return;
}
var valid = authenticationService.IsApiKeyValid(extractedApiKey);
if (!valid)
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Unauthorized client");
return;
}
await _next(context);
}
}
public sealed class AuthenticationService
{
private readonly ILogger<AuthenticationService> _logger;
private readonly IDbContextFactory<DataContext> _dbContextFactory;
private List<string> _apiKeys;

public AuthenticationService(ILogger<AuthenticationService> logger,IDbContextFactory<DataContext> dbContextFactory)
{
_dbContextFactory = dbContextFactory;
using var dataContext = _dbContextFactory.CreateDbContext();
_logger = logger;
_apiKeys = dataContext.ApiKey.Select(a => a.key).ToList();
_logger.LogDebug($"{this.GetType().Name} initialized!");
}

public async Task LoadApiKeys()
{
using var dataContext = _dbContextFactory.CreateDbContext();
_apiKeys = await dataContext.ApiKey.Select(a => a.key).ToListAsync();
}

public bool IsApiKeyValid(string apiKey)
{
return _apiKeys.Contains(apiKey);
}
}
public sealed class AuthenticationService
{
private readonly ILogger<AuthenticationService> _logger;
private readonly IDbContextFactory<DataContext> _dbContextFactory;
private List<string> _apiKeys;

public AuthenticationService(ILogger<AuthenticationService> logger,IDbContextFactory<DataContext> dbContextFactory)
{
_dbContextFactory = dbContextFactory;
using var dataContext = _dbContextFactory.CreateDbContext();
_logger = logger;
_apiKeys = dataContext.ApiKey.Select(a => a.key).ToList();
_logger.LogDebug($"{this.GetType().Name} initialized!");
}

public async Task LoadApiKeys()
{
using var dataContext = _dbContextFactory.CreateDbContext();
_apiKeys = await dataContext.ApiKey.Select(a => a.key).ToListAsync();
}

public bool IsApiKeyValid(string apiKey)
{
return _apiKeys.Contains(apiKey);
}
}
44 replies
CC#
Created by Welles on 9/22/2022 in #help
.net core webapi2 API key authorization
I could not find any information online about using api-keys as a header in conjunction with useauthentication/authorization
44 replies
CC#
Created by Welles on 9/22/2022 in #help
.net core webapi2 API key authorization
Well it is custom middleware to check the api key against the database. My app is not public, api is only for local use but still wanted to learn. So at the moment using a middleware that is calling a function in a singleton service
44 replies
CC#
Created by Welles on 9/15/2022 in #help
asp.net core services initialization
Well, thank you very much for the information here. I need to take a look on practical examples. 😄
32 replies
CC#
Created by Welles on 9/15/2022 in #help
asp.net core services initialization
the init code needs to run before the server accepts connections
32 replies
CC#
Created by Welles on 9/15/2022 in #help
asp.net core services initialization
As I see it now, using the factory pattern, I still need to await Factory.createAsync somewhere. And I can't do that in constructors, so the issue is the same no?
32 replies
CC#
Created by Welles on 9/15/2022 in #help
asp.net core services initialization
Ok gotcha, it's a lot to get my head around. Need to figure out to just not make it all a singleton and then use AddDbContextFactory instead of normal dbcontext which is scoped by default. A lot of things to figure out
32 replies
CC#
Created by Welles on 9/15/2022 in #help
asp.net core services initialization
but I don't see how this could work if I don't inject cardanofactory instead of cardanoservice in my controller. When does the createAsync method gets triggered? On requests? And I was also thinking, if scoped services are default with asp.net core stuff, isn't that like horrible for performance it needs to contruct each time a requests comes in?
32 replies
CC#
Created by Welles on 9/15/2022 in #help
asp.net core services initialization
yeah it is 🙂
32 replies
CC#
Created by Welles on 9/15/2022 in #help
asp.net core services initialization
instead of cardanoservice
32 replies
CC#
Created by Welles on 9/15/2022 in #help
asp.net core services initialization
haha omg, yeah I thinkl I know. I need to inject cardanofactory in the rest of them
32 replies