Y3S1-Cloud_Systems_DevOps/ThAmCo-Products/ThAmCo.Products/Startup.cs

103 lines
3.9 KiB
C#
Raw Permalink Normal View History

2020-06-09 20:21:37 +00:00
using System;
using System.IO;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Polly;
using ThAmCo.Products.Data;
using ThAmCo.Products.Data.ProductsContext;
namespace ThAmCo.Products
{
public class Startup
{
public Startup(IConfiguration configuration)
{
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)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddDbContext<ProductsDbContext>(options => options.UseSqlServer(
Configuration.GetConnectionString("ProductsSqlConnection"), optionsBuilder =>
optionsBuilder.EnableRetryOnFailure(3, TimeSpan.FromSeconds(10), null)));
services.AddHttpClient("StandardRequest")
.AddTransientHttpErrorPolicy(p =>
p.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))))
.AddTransientHttpErrorPolicy(p =>
p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));
services.AddHttpClient("ReviewRequest")
.AddTransientHttpErrorPolicy(p =>
p.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
.WaitAndRetryAsync(1, retryAttempt => TimeSpan.FromSeconds(2)));
services.AddAuthorization(options =>
{
options.AddPolicy("StaffOnly", builder =>
{
builder.RequireClaim("role", "Staff");
});
});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
options =>
{
options.LoginPath = new PathString("/account/login");
options.AccessDeniedPath = new PathString("/account/AccessDenied");
});
services.AddScoped<IProductsContext, ProductsContext>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment 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.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Products}/{action=Index}");
});
}
}
}