Files
carparkvalidation/CarParkValidationAPI/Startup.cs
T

150 lines
5.3 KiB
C#

using CarParkValidationAPI.Authorization;
using CarParkValidationAPI.Datas;
using CarParkValidationAPI.Entities;
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<AppSettings>(appSettingsSection);
var appSettings = appSettingsSection.Get<AppSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.Secret);
//***
services.AddDbContext<DataContext>();
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
};
});
//**
//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<IJwtUtils, JwtUtils>();
//services.AddScoped<IUserService, UserFakeRepository>();
services.AddScoped<IUserService, UserRepository>();
services.AddScoped<IConcessionType, ConcessionTypeRepository>();
services.AddScoped<IValidationPoint, ValidationPointRepository>();
services.AddScoped<IAdminUser, AdminUserRepository>();
services.AddScoped<IConcessionValidation, ConcessionValidationRepository>();
services.AddScoped<ILookupCode, LookupCodeRepository>();
services.AddScoped(typeof(ConcessionValidationReportRepository));
}
// 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.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<ErrorHandlerMiddleware>();
// custom jwt auth middleware
app.UseMiddleware<JwtMiddleware>();
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.
});
}
}
}