119 lines
4.3 KiB
C#

using Microsoft.EntityFrameworkCore;
using DigitalData.UserManager.Application.MappingProfiles;
using DigitalData.UserManager.Application.Contracts;
using DigitalData.UserManager.Application.Services;
using DigitalData.UserManager.Infrastructure.Repositories;
using DigitalData.UserManager.Infrastructure.Contracts;
using DigitalData.Core.CultureServices;
using DigitalData.Core.Application;
using Microsoft.AspNetCore.Authentication.Cookies;
using NLog.Web;
using NLog;
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
logger.Debug("init main");
try {
var builder = WebApplication.CreateBuilder(args);
if (builder.Configuration.GetValue<bool>("RunAsWindowsService"))
builder.Host.UseWindowsService();
builder.Logging.ClearProviders();
builder.Host.UseNLog();
builder.Services.AddControllers();
if (builder.Configuration.GetValue<bool>("UseSwagger"))
{
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
}
builder.Services.AddControllers();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.Cookie.HttpOnly = true;
//options.Cookie.SecurePolicy = CookieSecurePolicy.Always; //always https
options.Cookie.SameSite = SameSiteMode.None;
options.LoginPath = "/api/auth/login";
options.LogoutPath = "/api/auth/logout";
});
builder.Services.AddDbContext<DDECMDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DD_ECM_Connection"))
.EnableDetailedErrors());
var allowedOrigins = builder.Configuration.GetSection("AllowedOrigins").Get<string[]>() ?? throw new InvalidOperationException("In appsettings there is no allowed origin.");
builder.Services.AddCors(options =>
{
options.AddPolicy(name: "DefaultCorsPolicy",
builder =>
{
builder.WithOrigins(allowedOrigins)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
builder.Services.AddKeyTranslationService();
builder.Services.AddAutoMapper(typeof(UserMappingProfile).Assembly);
builder.Services.AddAutoMapper(typeof(GroupMappingProfile).Assembly);
builder.Services.AddAutoMapper(typeof(GroupOfUserMappingProfile).Assembly);
builder.Services.AddAutoMapper(typeof(ModuleMappingProfile).Assembly);
builder.Services.AddAutoMapper(typeof(ModuleOfUserMappingProfile).Assembly);
builder.Services.AddAutoMapper(typeof(UserRepMappingProfile).Assembly);
builder.Services.AddAutoMapper(typeof(DirectoryMappingProfile).Assembly);
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IGroupRepository, GroupRepository>();
builder.Services.AddScoped<IGroupOfUserRepository, GroupOfUserRepository>();
builder.Services.AddScoped<IModuleRepository, ModuleRepository>();
builder.Services.AddScoped<IModuleOfUserRepository, ModuleOfUserRepository>();
builder.Services.AddScoped<IUserRepRepository, UserRepRepository>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IGroupService, GroupService>();
builder.Services.AddScoped<IGroupOfUserService, GroupOfUserService>();
builder.Services.AddScoped<IModuleService, ModuleService>();
builder.Services.AddScoped<IModuleOfUserService, ModuleOfUserService>();
builder.Services.AddScoped<IUserRepService, UserRepService>();
builder.Services.AddDirectorySearchService();
builder.Services.AddResponseService();
var app = builder.Build();
app.UseCors("DefaultCorsPolicy");
if (builder.Configuration.GetValue<bool>("UseSwagger"))
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapDefaultControllerRoute();
app.Run();
}
catch (Exception exception)
{
logger.Error(exception, "Stopped program because of exception");
throw;
}