techgarage-ir.MTWireGuard/Application/ApplicationServiceRegister.cs

97 lines
3.4 KiB
C#
Raw Normal View History

2023-06-23 17:00:49 +03:30
using Hangfire;
using Hangfire.Storage.SQLite;
2024-01-25 20:38:47 +03:30
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Caching.Memory;
2023-06-23 17:00:49 +03:30
using Microsoft.Extensions.DependencyInjection;
2024-01-25 20:38:47 +03:30
using Microsoft.IdentityModel.Tokens;
2023-06-02 15:26:29 +03:30
using MTWireGuard.Application.Mapper;
using MTWireGuard.Application.Repositories;
using MTWireGuard.Application.Services;
using System.Reflection;
2024-01-25 20:38:47 +03:30
using System.Text;
2023-06-02 15:26:29 +03:30
namespace MTWireGuard.Application
{
public static class ApplicationServiceRegister
{
public static void AddApplicationServices(this IServiceCollection services)
{
// Add DBContext
2024-01-25 20:38:47 +03:30
services.AddDbContext<DBContext>();
2023-06-02 15:26:29 +03:30
2023-06-23 17:00:49 +03:30
// Add HangFire
services.AddHangfire(config =>
{
config.UseSQLiteStorage(Path.Join(AppDomain.CurrentDomain.BaseDirectory, "MikrotikWireguard.db"));
});
services.AddHangfireServer();
2023-06-02 15:26:29 +03:30
// Auto Mapper Configurations
services.AddSingleton<PeerMapping>();
services.AddSingleton<ServerMapping>();
services.AddSingleton<MappingProfile>();
2024-01-25 20:38:47 +03:30
services.AddSingleton<RequestProfile>();
2023-06-02 15:26:29 +03:30
services.AddAutoMapper(
2024-01-25 20:38:47 +03:30
(provider, expression) =>
{
2023-06-02 15:26:29 +03:30
expression.AddProfile(provider.GetService<PeerMapping>());
expression.AddProfile(provider.GetService<ServerMapping>());
expression.AddProfile(provider.GetService<MappingProfile>());
2024-01-25 20:38:47 +03:30
expression.AddProfile(provider.GetService<RequestProfile>());
2023-06-02 15:26:29 +03:30
},
new List<Assembly>());
// Add Mikrotik API Service
2024-01-25 20:38:47 +03:30
services.AddScoped<IMikrotikRepository, MTAPI>();
2023-06-23 17:00:49 +03:30
2024-01-25 20:38:47 +03:30
// XSRF Protection
services.AddAntiforgery(o =>
{
o.HeaderName = "XSRF-TOKEN";
o.FormFieldName = "XSRF-Validation-Token";
o.Cookie.Name = "XSRF-Validation";
});
// Authentication and Authorization
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(15);
options.SlidingExpiration = true;
options.LoginPath = "/Login";
options.AccessDeniedPath = "/Forbidden";
options.Cookie.Name = "Authentication";
options.LogoutPath = "/Logout";
});
services.ConfigureApplicationCookie(configure =>
{
configure.Cookie.Name = "MTWireguard";
});
services.AddAuthorization();
// Add Razor Pages
services.AddRazorPages().AddRazorPagesOptions(o =>
{
//o.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute());
o.Conventions.AuthorizeFolder("/");
o.Conventions.AllowAnonymousToPage("/Login");
});
// Add Session
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(1);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
// Add CORS
services.AddCors();
2023-06-02 15:26:29 +03:30
}
}
}