Files
Backend-Api/ServiceHost/Program.cs

190 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using _0_Framework.Application.Sms;
using _0_Framework.Application;
using AccountManagement.Configuration;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Features;
using PersonalContractingParty.Config;
using ServiceHost;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Query.Bootstrapper;
using ServiceHost.Hubs;
using ServiceHost.MiddleWare;
using WorkFlow.Infrastructure.Config;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages()
.AddRazorRuntimeCompilation();
//Register Services
#region Register Services
builder.Services.AddHttpContextAccessor();
builder.Services.AddHttpClient("holidayApi", c => c.BaseAddress = new System.Uri("https://api.github.com"));
var connectionString = builder.Configuration.GetConnectionString("MesbahDb");
PersonalBootstrapper.Configure(builder.Services, connectionString);
AccountManagementBootstrapper.Configure(builder.Services, connectionString);
WorkFlowBootstrapper.Configure(builder.Services, connectionString);
QueryBootstrapper.Configure(builder.Services);
builder.Services.AddSingleton<IPasswordHasher, PasswordHasher>();
builder.Services.AddTransient<IFileUploader, FileUploader>();
builder.Services.AddTransient<IAuthHelper, AuthHelper>();
builder.Services.AddTransient<IGoogleRecaptcha, GoogleRecaptcha>();
builder.Services.AddTransient<ISmsService, SmsService>();
//services.AddSingleton<IWorkingTest, WorkingTest>();
//services.AddHostedService<JobWorker>();
builder.Services.Configure<FormOptions>(options =>
{
options.ValueCountLimit = int.MaxValue;
options.KeyLengthLimit = int.MaxValue;
options.ValueLengthLimit = int.MaxValue;
options.MultipartBodyLengthLimit = long.MaxValue;
options.MemoryBufferThreshold = int.MaxValue;
options.MultipartHeadersLengthLimit = int.MaxValue;
});
builder.Services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.Strict;
});
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o =>
{
o.LoginPath = new PathString("/");
o.LogoutPath = new PathString("/index");
o.AccessDeniedPath = new PathString("/AccessDenied");
o.ExpireTimeSpan = TimeSpan.FromHours(10);
o.SlidingExpiration = true;
});
//services.AddAuthorization(options =>
// options.AddPolicy("AdminArea", builder =>builder.RequireRole(Roles.role)));
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("AdminArea",
builder => builder.RequireClaim("AccountId"));
options.AddPolicy("AdminArea",
builder => builder.RequireClaim("AdminAreaPermission", new List<string> { "true" }));
});
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("ClientArea",
builder => builder.RequireClaim("AccountId"));
options.AddPolicy("ClientArea",
builder => builder.RequireClaim("ClientAriaPermission", new List<string> { "true" }));
});
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("CameraArea",
builder => builder.RequireClaim("AccountId"));
});
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("AdminNewArea",
builder => builder.RequireClaim("AccountId"));
options.AddPolicy("AdminNewArea",
builder => builder.RequireClaim("AdminAreaPermission", new List<string> { "true" }));
});
//services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
// .AddCookie(option =>
// {
// option.LoginPath = "/Index";
// option.LogoutPath = "/Index";
// option.ExpireTimeSpan = TimeSpan.FromDays(1);
// });
builder.Services.AddRazorPages(options =>
options.Conventions.AuthorizeAreaFolder("Admin", "/", "AdminArea"));
builder.Services.AddRazorPages(options =>
options.Conventions.AuthorizeAreaFolder("Client", "/", "ClientArea"));
builder.Services.AddRazorPages(options =>
options.Conventions.AuthorizeAreaFolder("Camera", "/", "CameraArea"));
builder.Services.AddRazorPages(options =>
options.Conventions.AuthorizeAreaFolder("AdminNew", "/", "AdminNewArea"));
builder.Services.AddMvc();
builder.Services.AddSignalR();
#endregion
#region PWA
//old
//builder.Services.AddProgressiveWebApp();
//new
//builder.Services.AddProgressiveWebApp(new PwaOptions
//{
// RegisterServiceWorker = true,
// RegisterWebmanifest = true,
// Strategy = ServiceWorkerStrategy.NetworkFirst,
//});
#endregion
var app = builder.Build();
#region Mahan
app.UseStatusCodePagesWithRedirects("/error/{0}");
#endregion
//Create Http Pipeline
#region Create Http Pipeline
if (builder.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseDeveloperExceptionPage();
// The default HSTS value is 30 days. You may want to change this for pro
app.UseHsts();
}
app.UseAuthentication();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseRouting();
app.UseAuthorization();
#region Mahan
//app.UseLoginHandlerMiddleware();
//app.UseCheckTaskMiddleware();
#endregion
app.MapHub<CreateContractTarckingHub>("/trackingHub");
app.MapHub<SendAccountMessage>("/trackingSmsHub");
app.MapHub<HolidayApiHub>("/trackingHolidayHub");
app.MapHub<CheckoutHub>("/trackingCheckoutHub");
app.MapRazorPages();
app.MapControllers();
#endregion
app.Run();