Mih4n
Mih4n
CC#
Created by Mih4n on 7/30/2024 in #help
need to get data from ms access on linux. Help me pls
.
5 replies
CC#
Created by Mih4n on 6/26/2024 in #help
thumbnail gen(doc, excel)
Here again but now I need to generate doc and excel files thumbnails for free for business.
4 replies
CC#
Created by Mih4n on 5/9/2024 in #help
Microsoft Identity and web api
I need help with integrating microsoft account oauth into my api. I wanna bind it with account on my service to get users xbox data. But I steel can not find any good guide about it. I have .net api and vue3 front.
3 replies
CC#
Created by Mih4n on 5/5/2024 in #help
✅ I need in help. How to use razor as html templater.
I now using fluid as a templater but a lot of people says that razor is better alternative but I steel can not find any good guide about it.
5 replies
CC#
Created by Mih4n on 4/30/2024 in #help
open source mit pdf lib
I need to generate pdf report on my work but I an outsource worker and I can't use any lib that is licensed for business as paid. Please help P.s. Sory for my English)
8 replies
CC#
Created by Mih4n on 11/25/2023 in #help
Create thumbnail from pdf
I need advice on what library to choose. I just need to get a first page of a pdf file and than convert it into Image class
3 replies
CC#
Created by Mih4n on 10/17/2023 in #help
❔ I now writing auth for my api and I want to know can I make it better
using System.Security.Claims;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;
using MultiApi.Database;
using MultiApi.Database.Tables;

namespace MultiApi.Auth;

public class AuthanticationByBearerToken: AuthenticationHandler<AuthenticationSchemeOptions>
{
private AppDbContext dbContext;

public AuthanticationByBearerToken(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock,
AppDbContext dbContext) : base(options, logger, encoder, clock)
{
this.dbContext = dbContext;
}

protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (!Request.Headers.TryGetValue("Authorization", out var apiKeyHeaderValues))
{
return AuthenticateResult.Fail("API Key was not provided.");
}

string providedApiKey = apiKeyHeaderValues.FirstOrDefault();

if (providedApiKey != null && await IsValidApiKey(providedApiKey))
{
ApiKey apiKey = dbContext.ApiKeys.Find(providedApiKey.Replace("Bearer ", ""));
var claims = new[]
{
new Claim(ClaimTypes.Role, apiKey.type.ToString())
};
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, Scheme.Name);

return AuthenticateResult.Success(ticket);
}

return AuthenticateResult.Fail("Invalid API Key provided.");
}

private async Task<bool> IsValidApiKey(string apiKey)
{
return dbContext.ApiKeys.Any(key => key.Key == apiKey.Replace("Bearer ", ""));
}
}
using System.Security.Claims;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;
using MultiApi.Database;
using MultiApi.Database.Tables;

namespace MultiApi.Auth;

public class AuthanticationByBearerToken: AuthenticationHandler<AuthenticationSchemeOptions>
{
private AppDbContext dbContext;

public AuthanticationByBearerToken(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock,
AppDbContext dbContext) : base(options, logger, encoder, clock)
{
this.dbContext = dbContext;
}

protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (!Request.Headers.TryGetValue("Authorization", out var apiKeyHeaderValues))
{
return AuthenticateResult.Fail("API Key was not provided.");
}

string providedApiKey = apiKeyHeaderValues.FirstOrDefault();

if (providedApiKey != null && await IsValidApiKey(providedApiKey))
{
ApiKey apiKey = dbContext.ApiKeys.Find(providedApiKey.Replace("Bearer ", ""));
var claims = new[]
{
new Claim(ClaimTypes.Role, apiKey.type.ToString())
};
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, Scheme.Name);

return AuthenticateResult.Success(ticket);
}

return AuthenticateResult.Fail("Invalid API Key provided.");
}

private async Task<bool> IsValidApiKey(string apiKey)
{
return dbContext.ApiKeys.Any(key => key.Key == apiKey.Replace("Bearer ", ""));
}
}
3 replies