in UnicornStore/Startup.cs [32:110]
public void ConfigureServices(IServiceCollection services)
{
// The UNICORNSTORE_DBSECRET is stored in AWS Secrets Manager
// The value is loaded as an Environment Variable in a JSON string
// The key/value pairs are mapped to the Configuration
if (Configuration["UNICORNSTORE_DBSECRET"] != null)
{
var unicorn_envvariables = Configuration["UNICORNSTORE_DBSECRET"];
var document = JsonDocument.Parse(unicorn_envvariables);
var root = document.RootElement;
Configuration["UNICORNSTORE_DBSECRET:username"] = root.GetProperty("username").GetString();
Configuration["UNICORNSTORE_DBSECRET:password"] = root.GetProperty("password").GetString();
Configuration["UNICORNSTORE_DBSECRET:host"] = root.GetProperty("host").GetString();
}
var sqlconnectionbuilder = new SqlConnectionStringBuilder(
Configuration.GetConnectionString("UnicornStore"));
sqlconnectionbuilder.Password = Configuration["UNICORNSTORE_DBSECRET:password"];
sqlconnectionbuilder.UserID = Configuration["UNICORNSTORE_DBSECRET:username"];
sqlconnectionbuilder.DataSource = Configuration["UNICORNSTORE_DBSECRET:host"];
_connection = sqlconnectionbuilder.ConnectionString;
services.AddDbContext<UnicornStoreContext>(options =>
options.UseSqlServer(_connection));
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<UnicornStoreContext>()
.AddDefaultTokenProviders();
services.ConfigureApplicationCookie(options => options.AccessDeniedPath = "/Home/AccessDenied");
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder =>
{
builder.WithOrigins("http://example.com");
});
});
services.AddLogging();
// Add MVC services to the services container
services.AddControllersWithViews();
services.AddRazorPages();
services.AddOptions();
// Add the Healthchecks
// https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks
// AspNetCore.Diagnostics.HealthChecks isn't maintained or supported by Microsoft.
services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy())
.AddSqlServer(_connection,
name: "UnicornDB-check",
tags: new string[] { "UnicornDB" });
// Add memory cache services
services.AddMemoryCache();
services.AddDistributedMemoryCache();
// Add session related services.
services.AddSession();
// Add the system clock service
services.AddSingleton<ISystemClock, SystemClock>();
// Configure Auth
services.AddAuthorization(options =>
{
options.AddPolicy(
"ManageStore",
authBuilder =>
{
authBuilder.RequireClaim("ManageStore", "Allowed");
});
});
}