-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
121 lines (92 loc) · 3.87 KB
/
Program.cs
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
using System.Reflection;
using Fakestagram.Data;
using Fakestagram.Data.AutoMapperProfile;
using Fakestagram.Data.Repositories;
using Fakestagram.Data.Repositories.Contracts;
using Fakestagram.Services;
using Fakestagram.Services.Contracts;
using Fakestagram.Services.Helpers;
using Fakestagram.Services.Providers;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers().AddNewtonsoftJson();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
//Swagger UI with auth scheme support
builder.Services.AddSwaggerGen(opt =>
{
//Bearer token auth definition
opt.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Description = "Standard authorization header using the Bearer scheme ('Bearer {token}')",
In = ParameterLocation.Header,
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
opt.OperationFilter<SecurityRequirementsOperationFilter>();
//Enable XML additional documentation
string xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
string xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
opt.IncludeXmlComments(xmlPath);
}).AddSwaggerGenNewtonsoftSupport();
var tokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey =
new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(builder.Configuration.GetSection("JWTConfig").GetSection("TokenSecret").Value)),
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true
};
//Bearer Config
builder.Services.AddAuthentication(
JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(opt =>
{
opt.TokenValidationParameters = tokenValidationParameters;
});
builder.Services.AddDbContext<FakestagramDbContext>(opt => opt.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddAutoMapper(conf => conf.AddProfile(new AutoMapperProfile()));
// Repositories
builder.Services.AddScoped<IUsersRepository, UsersRepository>();
builder.Services.AddScoped<IFollowRepository, FollowRepository>();
builder.Services.AddScoped<IPostsRepository, PostsRepository>();
builder.Services.AddScoped<ICommentsRepository, CommentsRepository>();
builder.Services.AddScoped<IRefreshTokenRepository, TokenRepository>();
builder.Services.AddScoped<IPostLikesRepository, PostLikesRepository>();
builder.Services.AddScoped<ICommentLikesRepository, CommentLikesRepository>();
// Services
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IImageService, ImageService>();
builder.Services.AddScoped<IPostService, PostService>();
builder.Services.AddScoped<ILikesService, LikesService>();
builder.Services.AddScoped<ICommentsService, CommentsService>();
builder.Services.AddHttpContextAccessor();
// Provider-services
builder.Services.AddScoped<IAuthProvider, JwtAuthProvider>();
builder.Services.AddScoped<IPasswordProvider, SHA512PasswordProvider>();
// Helper-services
builder.Services.AddScoped<IJsonErrorSerializerHelper, JsonErrorSerializerHelper>();
builder.Services.AddScoped<IPaginationHelper, PaginationHelper>();
builder.Services.AddSingleton(tokenValidationParameters);
// Add webroot directory used for uploading the photos
builder.WebHost.UseWebRoot("wwwroot");
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.UseStaticFiles();
app.Run();