167 lines
4.8 KiB
C#
167 lines
4.8 KiB
C#
using Marco.Pms.DataAccess.Data;
|
|
using Marco.Pms.Model.Authentication;
|
|
using Marco.Pms.Model.Utilities;
|
|
using MarcoBMS.Services.Helpers;
|
|
using MarcoBMS.Services.Middleware;
|
|
using MarcoBMS.Services.Service;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi.Models;
|
|
using Serilog;
|
|
using System.Text;
|
|
|
|
Serilog.Debugging.SelfLog.Enable(Console.Error);
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
// Add Serilog Configuration
|
|
builder.Host.UseSerilog((context, config) =>
|
|
{
|
|
config.ReadFrom.Configuration(context.Configuration); // Taking all configuration from appsetting.json
|
|
|
|
})
|
|
;
|
|
|
|
// Add services
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("DevCorsPolicy", policy =>
|
|
{
|
|
policy.AllowAnyOrigin()
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader();
|
|
});
|
|
});
|
|
|
|
//builder.Services.AddCors(options =>
|
|
//{
|
|
// options.AddPolicy("ProdCorsPolicy", policy =>
|
|
// {
|
|
// policy.WithOrigins("https://yourdomain.com", "https://anothertrustedsource.com") // List of allowed origins
|
|
// .AllowAnyMethod()
|
|
// .AllowAnyHeader();
|
|
// });
|
|
//});
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddControllers();
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
builder.Services.AddSwaggerGen(option =>
|
|
{
|
|
option.SwaggerDoc("v1", new OpenApiInfo { Title = "Demo API", Version = "v1" });
|
|
option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
{
|
|
In = ParameterLocation.Header,
|
|
Description = "Please enter a valid token",
|
|
Name = "Authorization",
|
|
Type = SecuritySchemeType.Http,
|
|
BearerFormat = "JWT",
|
|
Scheme = "Bearer"
|
|
});
|
|
|
|
option.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Type=ReferenceType.SecurityScheme,
|
|
Id="Bearer"
|
|
}
|
|
},
|
|
new string[]{}
|
|
}
|
|
});
|
|
});
|
|
|
|
builder.Services.Configure<SmtpSettings>(builder.Configuration.GetSection("EmailSettings"));
|
|
builder.Services.AddTransient<IEmailSender, EmailSender>();
|
|
|
|
builder.Services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
|
|
|
|
|
|
string connString = builder.Configuration.GetConnectionString("DefaultConnectionString");
|
|
|
|
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
|
{
|
|
options.UseMySql(connString, ServerVersion.AutoDetect(connString));
|
|
});
|
|
|
|
builder.Services.AddMemoryCache();
|
|
|
|
|
|
//builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
|
|
//builder.Services.AddScoped<IProjectRepository, ProjectRepository>();
|
|
//builder.Services.AddScoped<IEmployeeRepository, EmployeeRepository>();
|
|
//builder.Services.AddScoped<IActivityMasterRepository, ActivityMasterRepository>();
|
|
//builder.Services.AddScoped<IAttendenceRepository, AttendenceRepository>();
|
|
//builder.Services.AddScoped<IProjectAllocationRepository, ProjectAllocationRepository>();
|
|
|
|
builder.Services.AddScoped<RefreshTokenService>();
|
|
|
|
builder.Services.AddScoped<UserHelper>();
|
|
builder.Services.AddScoped<RolesHelper>();
|
|
builder.Services.AddScoped<EmployeeHelper>();
|
|
builder.Services.AddScoped<ProjectsHelper>();
|
|
builder.Services.AddSingleton<ILoggingService, LoggingService>();
|
|
|
|
|
|
builder.Services.AddHttpContextAccessor();
|
|
|
|
var jwtSettings = builder.Configuration.GetSection("Jwt").Get<JwtSettings>();
|
|
builder.Services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = jwtSettings.Issuer,
|
|
ValidAudience = jwtSettings.Audience,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.Key))
|
|
};
|
|
});
|
|
|
|
builder.Services.AddSingleton(jwtSettings);
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
// Use CORS in the pipeline
|
|
|
|
}
|
|
app.UseCors("DevCorsPolicy");
|
|
|
|
app.UseStaticFiles(); // Enables serving static files
|
|
|
|
app.UseSerilogRequestLogging(); // Log HTTP requests
|
|
app.UseHttpsRedirection();
|
|
app.UseMiddleware<ExceptionHandlingMiddleware>();
|
|
app.UseMiddleware<TenantMiddleware>();
|
|
app.UseMiddleware<LoggingMiddleware>();
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|