Skip to content

Instantly share code, notes, and snippets.

@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())
@TechWatching
TechWatching / BuildWebHostWithMSI.cs
Created February 17, 2019 16:05
WebHost with MSI (no client credentials)
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureAppConfiguration((builderContext, config) =>
{
var builtConfig = config.Build();
var vaultUrl = $"https://{buiItConfig["VaultName"]}.vault.azure.net/";
config.AddAzureKeyVault(vaultUrl);
});
@TechWatching
TechWatching / swapi.http
Created March 4, 2019 06:10
HTTP requests for the Star Wars API
#### List of start wars planets
GET https://swapi.co/api/planets/ HTTP/1.1
### Get Luke Skywalker
# @name lukeRequest
GET https://swapi.co/api/people/?search=Luke HTTP/1.1
### Get Luke Skywalker home planet
GET {{lukeRequest.response.body.results[0].homeworld}} HTTP/1.1
@TechWatching
TechWatching / TestingHandlebars.csx
Created March 9, 2019 15:27
C# script that use the Handlebars.Net library to do HTML templating
#r "nuget: Handlebars.Net, 1.9.5"
using HandlebarsDotNet;
string html = @"
<ul class=""people"">
{{#each people}}
<li>{{FirstName}} {{LastName}} - {{Job}}</li>
{{/each}}
</ul>";
public class UserService : IUserService
{
private readonly HttpClient _httpClient;
public UserService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<IReadOnlyCollection<User>> GetAllUsers()
{
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 class UserService :
{
private readonly HttpClient _httpClient;
public UserService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<IReadOnlyCollection<User>> GetAllUsers()
{
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>();