-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
151 lines (135 loc) · 5.83 KB
/
Startup.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// ---------------------------------------------------------------
// Copyright (c) the Gulchexra Burxonova
// Talent Management
// ---------------------------------------------------------------
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.OData;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using TalentManagement.Core.Brokers.DateTimes;
using TalentManagement.Core.Brokers.Emails;
using TalentManagement.Core.Brokers.Loggings;
using TalentManagement.Core.Brokers.Storages;
using TalentManagement.Core.Brokers.Tokens;
using TalentManagement.Core.Services.Foundations.Emails;
using TalentManagement.Core.Services.Foundations.Securities;
using TalentManagement.Core.Services.Foundations.Users;
using TalentManagement.Core.Services.Orchestrations;
using TalentManagement.Core.Services.Processings.Users;
using Tarteeb.Api.Brokers.Tokens;
namespace TalentManagement.Core
{
public class Startup
{
public Startup(IConfiguration configuration) =>
Configuration = configuration;
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddOData(options => options.Select().Filter().OrderBy());
services.AddDbContext<StorageBroker>();
services.AddCors(option =>
{
option.AddPolicy("MyPolicy", config =>
{
config.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
RegisterBrokers(services);
AddFoundationServices(services);
AddProcessingServices(services);
AddOrchestrationServices(services);
RegisterJwtConfigurations(services, Configuration);
services.AddSwaggerGen(config =>
{
config.SwaggerDoc("v1", new OpenApiInfo { Title = "TalentManagement.Core", Version = "v1" });
config.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please enter token",
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "bearer"
});
config.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type=ReferenceType.SecurityScheme,
Id="Bearer"
}
},
new string[]{}
}
});
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment environment)
{
if (environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(config => config.SwaggerEndpoint(
"/swagger/v1/swagger.json",
"TalentManagement.Core v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors("MyPolicy");
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
endpoints.MapControllers());
}
private static void RegisterBrokers(IServiceCollection services)
{
services.AddTransient<IStorageBroker, StorageBroker>();
services.AddTransient<ILoggingBroker, LoggingBroker>();
services.AddTransient<IDateTimeBroker, DateTimeBroker>();
services.AddTransient<ITokenBroker, TokenBroker>();
services.AddTransient<IEmailBroker, EmailBroker>();
}
private static void AddFoundationServices(IServiceCollection services)
{
services.AddTransient<IUserService, UserService>();
services.AddTransient<ISecurityService, SecurityService>();
services.AddTransient<IEmailService, EmailService>();
}
private static void AddProcessingServices(IServiceCollection services) =>
services.AddTransient<IUserProcessingService, UserProcessingService>();
private static void AddOrchestrationServices(IServiceCollection services) =>
services.AddTransient<IUserSecurityOrchestrationService, UserSecurityOrchestrationService>();
private static void RegisterJwtConfigurations(IServiceCollection services,
IConfiguration configuration)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
string key = configuration.GetSection("Jwt").GetValue<string>("Key");
byte[] convertKeyToBytes = Encoding.UTF8.GetBytes(key);
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(convertKeyToBytes),
ValidateIssuer = false,
ValidateAudience = false,
RequireExpirationTime = true,
ValidateLifetime = true
};
});
}
}
}