-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
108 lines (90 loc) · 3.8 KB
/
Program.cs
File metadata and controls
108 lines (90 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using ServiceCollectionAPI.Repositories;
using ServiceCollectionAPI.Repositories.Interfaces;
using ServiceCollectionAPI.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using ServiceCollectionAPI.Services.Interfaces;
using ServiceCollectionAPI.Services.Interfaces.UserManagement;
using Microsoft.OpenApi.Models;
using System.Text.Json;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Hosting;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = "{YOUR_URL}";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = "{YOUR_URL}",
ValidateAudience = true,
ValidAudience = "{YOUR_AUDIENCE}}",
ValidateLifetime = true
};
});
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("RequireAuthorization", policy =>
{
if (builder.Configuration.GetValue<bool>("IsProduction"))
{
policy.RequireAuthenticatedUser();
}
else
{
policy.Requirements.Add(new AllowAnonymousRequirement());
}
});
});
builder.Services.AddSingleton<IAuthorizationHandler, AllowAnonymousHandler>();
// Configuration
ConfigurationManager configuration = builder.Configuration;
//Repositories
builder.Services.AddTransient(typeof(IMongoRepository<>), typeof(MongoRepository<>));
//Services
builder.Services.AddTransient(typeof(IUserService), typeof(UserService));
builder.Services.AddTransient(typeof(ITenantService), typeof(TenantService));
builder.Services.AddTransient(typeof(IRoleService), typeof(RoleService));
builder.Services.AddTransient(typeof(IProductService), typeof(ProductService));
builder.Services.AddTransient(typeof(IEmployeeService), typeof(EmployeeService));
builder.Services.AddScoped(typeof(ITenantContextService), typeof(TenantContextService));
builder.Services.AddScoped(typeof(IEmailHelper), typeof(EmailHelper));
builder.Services.AddTransient(typeof(IPackageService), typeof(PackageService));
builder.Services.AddTransient(typeof(IOfferService), typeof(OfferService));
builder.Services.AddTransient(typeof(IClientOfferService), typeof(ClientOfferService));
builder.Services.AddTransient(typeof(IClientService), typeof(ClientService));
builder.Services.AddTransient(typeof(IInvoiceService), typeof(InvoiceService));
builder.Services.AddTransient(typeof(IContractService), typeof(ContractService));
builder.Services.AddTransient(typeof(IBusinessService), typeof(BusinessService));
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; // Disable the default PascalCase naming policy
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true; // Make the property names case-insensitive
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Service Collection API", Version = "v0" });
options.SchemaFilter<LowerCaseSchemaFilter>();
});
//Automapper
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseRouting();
app.UseAuthentication();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Service Collection API");
});
app.UseAuthorization();
app.MapControllers();
app.Run();