Files
carparkvalidation/API/Startup.cs
T
2026-04-22 08:48:04 +10:00

140 lines
4.7 KiB
C#

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<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
};
});
// services.AddAutoMapper(typeof(Startup));
//**
//publish need to take comment out
services.AddSwaggerGen();
/*
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));
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();
}
*/
// global cors policy
app.UseCors(x => x
.SetIsOriginAllowed(origin => true)
.AllowAnyMethod()
.AllowAnyHeader()
.WithExposedHeaders("Content-Disposition")
.AllowCredentials());
app.UseMiddleware<JwtMiddleware>();
app.UseMiddleware<ErrorHandlerMiddleware>(); // use this exception
app.UseSwagger();
app.UseSwaggerUI();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseAuthentication();
app.UseAuthorization();
}
}
}