Skip to content

Instantly share code, notes, and snippets.

@aidangarnish
Last active July 31, 2020 09:53
Show Gist options
  • Select an option

  • Save aidangarnish/6ae455a38500d02599b9a787ce051a24 to your computer and use it in GitHub Desktop.

Select an option

Save aidangarnish/6ae455a38500d02599b9a787ce051a24 to your computer and use it in GitHub Desktop.
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using VSTestKeyVault.Models;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Azure.Core;
using System.Data.SqlClient;
using VSTestKeyVault.Database;
using Microsoft.EntityFrameworkCore;
namespace VSTestKeyVault
{
public class Startup
{
private IWebHostEnvironment CurrentEnvironment{ get; set; }
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
CurrentEnvironment = env;
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
AppConfig appConfig = Configuration.GetSection("AppSettings").Get<AppConfig>();
services.AddSingleton(appConfig);
SecretConfig secretConfig = new SecretConfig();
services.AddControllersWithViews();
var builder = new SqlConnectionStringBuilder(
Configuration.GetConnectionString("BeautifulLoopProcessing"));
if(CurrentEnvironment.IsDevelopment())
{
//if the environment is development then fetch secrets from the Secret Manager
secretConfig = Configuration.Get<SecretConfig>();
builder.Password = Configuration["BeautifulLoopProcessingDbPassword"];
}
else
{
//otherwise fetch secrets from Azure Key Vault
SecretClientOptions options = new SecretClientOptions()
{
Retry =
{
Delay= TimeSpan.FromSeconds(2),
MaxDelay = TimeSpan.FromSeconds(16),
MaxRetries = 5,
Mode = RetryMode.Exponential
}
};
var client = new SecretClient(new Uri(appConfig.AzureKeyVault), new DefaultAzureCredential(),options);
KeyVaultSecret secret = client.GetSecret("MySecret");
secretConfig.MySecret = secret.Value;
KeyVaultSecret dbPasswordSecret = client.GetSecret("BeautifulLoopProcessingDbPassword");
builder.Password = dbPasswordSecret.Value;
}
services.AddSingleton(secretConfig);
string connection = builder.ConnectionString;
services.AddDbContext<ManagementReportingContext>(
options => options.UseSqlServer(connection));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment