Skip to content

Instantly share code, notes, and snippets.

View TechWatching's full-sized avatar

Alexandre Nédélec TechWatching

View GitHub Profile
public class UserApiAuthenticationService : IUserApiAuthenticationService
{
private readonly IMemoryCache _memoryCache;
private readonly HttpClient _httpClient;
public UserApiAuthenticationService(HttpClient httpClient, IMemoryCache memoryCache)
{
_httpClient = httpClient;
_memoryCache = memoryCache;
}
public class UserApiAuthenticationHandler : DelegatingHandler
{
private readonly IUserApiAuthenticationService _authenticationService;
public UserApiAuthenticationHandler(IUserApiAuthenticationService authenticationService)
{
_authenticationService = authenticationService;
}
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationTokencancellationToken)
public void ConfigureServices(IServiceCollectionservices)
{
services.AddMemoryCache();
services.AddHttpClient<IUserApiAuthenticationService, UserApiAuthenticationService>()
.ConfigureHttpClient(c => c.BaseAddress ="http://urltotheuserapi.com");
services.AddTransient<UserApiAuthenticationHandler>();
services.AddHttpClient<UserService, UserService>()
.ConfigureHttpClient(c => c.BaseAddress ="http://urltotheuserapi.com")
.AddHttpMessageHandler<UserApiAuthenticationHanler>();
public class AuthResponse
{
public string Token { get; set; }
public DateTime Expiration { get; set; }
}
public interface IUserService
{
Task<IReadOnlyCollection<User>> GetAllUsers();
Task UpdateUser(User userToUpdate);
}
var body = new { login = "login", password = "password" };
var response = await _httpClient.PostAsync("login", new StringContent(JsonConvert.SerializeObject(body)));
var authResponse = await response.Content.ReadAsAsync<AuthResponse>();
public class UserService :
{
private readonly HttpClient _httpClient;
public UserService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<IReadOnlyCollection<User>> GetAllUsers()
{
public class UserService : IUserService
{
private readonly HttpClient _httpClient;
public UserService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<IReadOnlyCollection<User>> GetAllUsers()
{
@TechWatching
TechWatching / KeyVault.Program.cs
Last active January 28, 2020 11:04
Nouveau projet ASP.NET Core 2.2
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
@TechWatching
TechWatching / BuildWebHost.cs
Last active January 28, 2020 10:46
BuildWebHost with KeyVault and client credentials
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureAppConfiguration((builderContext, config) =>
{
var env = hostingContext.HostingEnvironment;
var builtConfig = config.Build();
var vaultUrl = $"https://{buiItConfig["VaultName"]}.vault.azure.net/";
if (env.IsDevelopment())