-
Notifications
You must be signed in to change notification settings - Fork 5
/
Startup.cs
183 lines (153 loc) · 7.38 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using DSM.UI.Api.Helpers;
using DSM.UI.Api.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using MongoDB.Driver;
using Newtonsoft.Json;
using System.Text;
using System.Threading.Tasks;
namespace DSM.UI.Api
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddControllers();
services.AddDbContext<DSMAuthDbContext>(options => options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("DSMAuthServer")), ServiceLifetime.Transient);
services.AddDbContext<DSMStorageDataContext>(options => options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("DSMStorageServer")));
// Configure Settings object
var appSettingsSection = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettingsSection);
var cacheDbSettingsSection = this.Configuration.GetSection(nameof(CacheDBSettings));
services.Configure<CacheDBSettings>(cacheDbSettingsSection);
var cachingSetings = cacheDbSettingsSection.Get<CacheDBSettings>();
services.Configure<FTPSettings>(Configuration.GetSection("FTPSettings"));
var client = new MongoClient(cachingSetings.ConnectionString);
var database = client.GetDatabase(cachingSetings.DatabaseName);
Helpers.Caching.CacheHelper.CacheDatabase = database;
// Configure JWT Auth
var appSettings = appSettingsSection.Get<AppSettings>();
Helpers.AzureDevOps.RequestHelper.AzureDevOpsToken = appSettings.AzureDevOpsToken;
Helpers.AzureDevOps.RequestHelper.AzureDevOpsOrganizationName = appSettings.AzureDevOpsOrganizationName;
var key = Encoding.ASCII.GetBytes(appSettings.Secret);
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.Events = new JwtBearerEvents
{
OnTokenValidated = context =>
{
var userService = context.HttpContext.RequestServices.GetRequiredService<IUserService>();
var userName = context.Principal.Identity.Name;
var user = userService.GetByUserName(userName);
if (user == null || !user.Enabled)
{
context.Fail("Unauthorized");
}
return Task.CompletedTask;
}
};
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
x.IncludeErrorDetails = true;
});
services.AddMvcCore()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddNewtonsoftJson(opt => opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "DSM API",
Description = "Api for DSM UI",
});
var jwtSecurityScheme = new OpenApiSecurityScheme
{
Scheme = "bearer",
BearerFormat = "JWT",
Name = "JWT Authentication",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Description = "Put **_ONLY_** your JWT Bearer token on textbox below!(Ba��na bearer yazmadan sadece tokeni yap��t�r�n.)",
Reference = new OpenApiReference
{
Id = JwtBearerDefaults.AuthenticationScheme,
Type = ReferenceType.SecurityScheme
}
};
c.AddSecurityDefinition(jwtSecurityScheme.Reference.Id, jwtSecurityScheme);
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{ jwtSecurityScheme, System.Array.Empty<string>() }
});
});
services.AddScoped<IUserService, UserService>();
services.AddScoped<ISiteService, SiteService>();
services.AddScoped<IServerService, ServerService>();
services.AddScoped<ICompanyService, CompanyService>();
services.AddScoped<IReportsService, ReportsService>();
services.AddScoped<IDashboardService, DashboardService>();
services.AddScoped<IWebAccessLogService, WebAccessLogService>();
services.AddScoped<IAzureDevOpsService, AzureDevOpsService>();
services.AddScoped<IDatabasePortalService, DatabasePortalService>();
services.AddScoped<IMonitoringService, MonitoringService>();
services.AddScoped<IResponsibleService, ResponsibleService>();
services.AddScoped<ICustomerTrackingService, CustomerTrackingService>();
services.AddScoped<IInventoryTrackingService, InventoryTrackingService>();
services.AddScoped<ICustomService, CustomService>();
services.AddScoped<ICustomInventoryService, CustomInventoryService>();
services.AddScoped<IFileUploadService, FileUploadService>();
services.AddScoped<IDSMOperationLogger, DSMOperationLogger>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Import custom web access log midleware
app.UseWebAccessLog();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "DSM API V1");
});
app.UseRouting();
app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}