-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
145 lines (120 loc) · 5.56 KB
/
Program.cs
File metadata and controls
145 lines (120 loc) · 5.56 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using Azure.Storage.Blobs;
using FluentValidation;
using FluentValidation.AspNetCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Reamp.Data;
using Reamp.Domain.Models;
using Reamp.Interfaces;
using Reamp.Mappers;
using Reamp.Repositories;
using Reamp.Services;
using Reamp.Validator;
using Serilog;
using System.Security.Claims;
using System.Text;
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
// 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.AddControllers().AddJsonOptions(x =>
x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);
builder.Services.AddIdentity<User,UserRole>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.RequireHttpsMetadata=false;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Jwt:Issuer"],
ValidAudience = builder.Configuration["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"])),
NameClaimType = ClaimTypes.Name,
RoleClaimType = "scope"
};
});
builder.Services.AddAuthorizationBuilder()
.AddPolicy("AdminPolicy", policy => policy.RequireClaim("scope", "Admin"))
.AddPolicy("AgentPolicy", policy => policy.RequireClaim("scope", "Agent"))
.AddPolicy("PhotographyCompanyPolicy", policy => policy.RequireClaim("scope", "PhotographyCompany"));
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.AddDebug();
Log.Logger =new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File("logs/log.txt", rollingInterval:RollingInterval.Day)
.CreateLogger();
builder.Host.UseSerilog();
builder.Services.AddValidatorsFromAssemblyContaining<AddListingCaseDtoValidator>();
builder.Services.AddValidatorsFromAssemblyContaining<UpdateListingCaseDtoValidator>();
builder.Services.AddFluentValidationAutoValidation();
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("default")));
builder.Services.Configure<MongoDbSettings>(builder.Configuration.GetSection("MongoDbSettings"));
builder.Services.AddSingleton<MongoDbContext>();
builder.Services.AddSingleton(x =>
{
var config = x.GetRequiredService<IConfiguration>();
var connectionstring = config["AzureBlobStorage:ConnectionString"];
return new BlobServiceClient(connectionstring);
});
builder.Services.AddSingleton<IAzureBlobStorageService, AzureBlobStorageService>();
builder.Services.AddAutoMapper(typeof(MappingProfile).Assembly);
builder.Services.AddTransient<IEmailSenderService, EmailSenderService>();
builder.Services.AddScoped<IListingCaseRepository, ListingCaseRepository>();
builder.Services.AddScoped<IListingCaseService, ListingCaseService>();
builder.Services.AddScoped(typeof(IRepository<MediaAsset,Guid>), typeof(Repository<MediaAsset, Guid>));
builder.Services.AddScoped<IMediaAssetRepository, MediaAssetRepository>();
builder.Services.AddScoped<IMediaAssetService, MediaAssetService>();
builder.Services.AddScoped(typeof(IRepository<CaseContact, int>), typeof(Repository<CaseContact, int>));
builder.Services.AddScoped<ICaseContactRepository, CaseContactRepository>();
builder.Services.AddScoped<ICaseContactService, CaseContactService>();
builder.Services.AddScoped(typeof(IRepository<Agent, string>), typeof(Repository<Agent, string>));
builder.Services.AddScoped<IAgentService, AgentService>();
builder.Services.AddScoped(typeof(IRepository<PhotographyCompany, string>), typeof(Repository<PhotographyCompany, string>));
builder.Services.AddScoped<IPhotographyCompanyService, PhotographyCompanyService>();
builder.Services.AddScoped(typeof(IRepository<AgentPhotographyCompany, int>), typeof(Repository<AgentPhotographyCompany, int>));
builder.Services.AddScoped<IAgentPhotographyCompanyService, AgentPhotographyCompanyService>();
builder.Services.AddScoped(typeof(IRepository<AgentListingCase, int>), typeof(Repository<AgentListingCase, int>));
builder.Services.AddScoped<IAgentListingCaseRepository, AgentListingCaseRepository>();
builder.Services.AddScoped<IAgentListingCaseService, AgentListingCaseService>();
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowFrontend",
policy => policy
.WithOrigins("http://localhost:5173")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//app.UseMiddleware<ExceptionMiddleware>();
app.UseHttpsRedirection();
app.UseCors("AllowFrontend");
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();