using CarParkValidationAPI.Authorization; using CarParkValidationAPI.Datas; using CarParkValidationAPI.Entities; using CarParkValidationAPI.Helpers; using CarParkValidationAPI.Interface; using CarParkValidationAPI.Models; using CarParkValidationAPI.Repository; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CarParkValidationAPI { 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.AddControllers(); //** security var appSettingsSection = Configuration.GetSection("AppSettings"); services.Configure(appSettingsSection); var appSettings = appSettingsSection.Get(); var key = Encoding.ASCII.GetBytes(appSettings.Secret); //*** services.AddDbContext(); services.AddCors(); //*** services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(x => { x.RequireHttpsMetadata = false; x.SaveToken = true; x.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = false, ValidateAudience = false }; }); services.AddAutoMapper(typeof(Startup)); //** //publish need to take comment out services.AddSwaggerGen(c => { // c.ResolveConflictingActions(api => api.First()); c.SwaggerDoc("v1", new OpenApiInfo { Title = "CarParkValidationAPI", Version = "v1" }); }); /* services.AddSwaggerGen(option => { option.UseAllOfForInheritance(); option.UseAllOfToExtendReferenceSchemas(); option.IgnoreObsoleteActions(); option.IgnoreObsoleteProperties(); option.ResolveConflictingActions(api => api.First()); }); */ services.AddScoped(); //services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(typeof(ConcessionValidationReportRepository)); services.AddHttpContextAccessor(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DataContext context) { /* if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } */ app.UseMiddleware(); // use this exception app.UseSwagger(); app.UseSwaggerUI(c => { c.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None); c.SwaggerEndpoint("/swagger/v1/swagger.json", "CarParkValidationAPI v1"); }); app.UseRouting(); // global cors policy app.UseCors(x => x .SetIsOriginAllowed(origin => true) .AllowAnyMethod() .AllowAnyHeader() .WithExposedHeaders("Content-Disposition") .AllowCredentials()); // global error handler //app.UseMiddleware(); // custom jwt auth middleware app.UseMiddleware(); app.UseAuthorization(); /* if add angular under wwwroot then two line below needed*/ /***************************/ // app.UseDefaultFiles(); //it will use index.html <------------------ // app.UseStaticFiles(); //it for angular <------------------------- /***************************/ app.UseEndpoints(endpoints => { endpoints.MapControllers(); // endpoints.MapFallbackToController("Index", "Fallback"); //<------------------ put angular in wwwroot. }); } } }