using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.UI; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.eShopWeb.ApplicationCore.Interfaces; using Microsoft.eShopWeb.ApplicationCore.Services; using Microsoft.eShopWeb.Infrastructure.Data; using Microsoft.eShopWeb.Infrastructure.Identity; using Microsoft.eShopWeb.Infrastructure.Logging; using Microsoft.eShopWeb.Infrastructure.Services; using Microsoft.eShopWeb.RazorPages.Interfaces; using Microsoft.eShopWeb.RazorPages.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; using System.Text; namespace Microsoft.eShopWeb.RazorPages { public class Startup { private IServiceCollection _services; public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureDevelopmentServices(IServiceCollection services) { // use in-memory database ConfigureTestingServices(services); // use real database // ConfigureProductionServices(services); } public void ConfigureTestingServices(IServiceCollection services) { // use in-memory database services.AddDbContext(c => c.UseInMemoryDatabase("Catalog")); // Add Identity DbContext services.AddDbContext(options => options.UseInMemoryDatabase("Identity")); ConfigureServices(services); } public void ConfigureProductionServices(IServiceCollection services) { // use real database services.AddDbContext(c => { try { // Requires LocalDB which can be installed with SQL Server Express 2016 // https://www.microsoft.com/en-us/download/details.aspx?id=54284 c.UseSqlServer(Configuration.GetConnectionString("CatalogConnection")); } catch (System.Exception ex) { var message = ex.Message; } }); // Add Identity DbContext services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("IdentityConnection"))); ConfigureServices(services); } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { ConfigureCookieOptions(services); services.AddIdentity() .AddDefaultUI(UIFramework.Bootstrap4) .AddEntityFrameworkStores() .AddDefaultTokenProviders(); services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>)); services.AddScoped(typeof(IAsyncRepository<>), typeof(EfRepository<>)); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.Configure(Configuration); services.AddSingleton(new UriComposer(Configuration.Get())); services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>)); services.AddTransient(); // Add memory cache services services.AddMemoryCache(); services.AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Version_2_2) .AddRazorPagesOptions(options => { options.Conventions.AuthorizeFolder("/Order"); options.Conventions.AuthorizePage("/Basket/Checkout"); }); _services = services; } private static void ConfigureCookieOptions(IServiceCollection services) { services.Configure(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.ConfigureApplicationCookie(options => { options.Cookie.HttpOnly = true; options.ExpireTimeSpan = TimeSpan.FromHours(1); options.LoginPath = "/Account/Signin"; options.LogoutPath = "/Account/Signout"; }); } // 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(); ListAllRegisteredServices(app); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/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(); } private void ListAllRegisteredServices(IApplicationBuilder app) { app.Map("/allservices", builder => builder.Run(async context => { var sb = new StringBuilder(); sb.Append("

All Services

"); sb.Append(""); sb.Append(""); sb.Append(""); foreach (var svc in _services) { sb.Append(""); sb.Append($""); sb.Append($""); sb.Append($""); sb.Append(""); } sb.Append("
TypeLifetimeInstance
{svc.ServiceType.FullName}{svc.Lifetime}{svc.ImplementationType?.FullName}
"); await context.Response.WriteAsync(sb.ToString()); })); } } }