-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
236 lines (195 loc) · 9.37 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Logging;
using Microsoft.Identity.Web;
using Serilog;
using Inventory.Models;
using Inventory.Services;
using Inventory.Utilities;
using Inventory.Validations.CategoryValidations;
using Inventory.Validations.DocumentTypeValidations;
using Inventory.Validations.DocumentValidations;
using Inventory.Validations.ItemTemplateValidations;
using Inventory.Validations.ItemValidations;
using Inventory.Validations.ListValidations;
using Inventory.Validations.LocationValidations;
using Inventory.Validations.PreCheckValidations;
using Inventory.Validations.SizeValidations;
using Inventory.Validations.VendorValidations;
using Microsoft.OpenApi.Models;
using Infrastructure.Persistence.ServiceBus;
using Azure.Messaging.ServiceBus;
namespace Inventory;
public class Startup(IConfiguration configuration)
{
private IConfiguration Configuration { get; } = configuration;
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.SuppressAsyncSuffixInActionNames = false;
});
IdentityModelEventSource.ShowPII = true;
ConfigureAuthenticationAndAuthorization(services);
services.AddIdentityServer()
.AddSigningCredentials();
// Add CORS services
services.AddCors(options =>
{
options.AddPolicy("AllowAllHeaders",
builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
});
var azureCredentials = new DefaultAzureCredential();
services.AddSingleton<ServiceBusClient>(serviceProvider =>
{
string serviceBusNamespace = Configuration["ServiceBus:Namespace"] ?? throw new Exception("Missing namespace in configuration");
return new ServiceBusClient($"{serviceBusNamespace}.servicebus.windows.net", azureCredentials);
});
services.AddSwaggerGen(c =>
{
c.EnableAnnotations();
c.AddSecurityRequirement(new OpenApiSecurityRequirement() {
{
new OpenApiSecurityScheme {
Reference = new OpenApiReference {
Type = ReferenceType.SecurityScheme,
Id = "oauth2"
},
Scheme = "oauth2",
Name = "oauth2",
In = ParameterLocation.Header,
},
new List <string> ()
}
});
c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Name = "oauth2",
Description = "OAuth2.0 auth code with implicit grant",
Flows = new OpenApiOAuthFlows
{
Implicit = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri("https://login.microsoftonline.com/91020923-529b-458c-8796-98af46bf6003/oauth2/v2.0/authorize"),
TokenUrl = new Uri("https://login.microsoftonline.com/91020923-529b-458c-8796-98af46bf6003/oauth2/v2.0/token"),
Scopes = new Dictionary<string, string>{
{ "api://a053137b-4efd-46a8-bf41-7ed3fc49f3be/Inventory.ReadAll", "access inventory api" }
}
}
},
});
});
services.AddScoped<IUserService, UserService>();
services.AddScoped<IItemService, ItemService>();
services.AddScoped<IListService, ListService>();
services.AddScoped<ICategoryService, CategoryService>();
services.AddScoped<ILocationService, LocationService>();
services.AddScoped<IVendorService, VendorService>();
services.AddScoped<IDocumentService, DocumentService>();
services.AddScoped<IItemTemplateService, ItemTemplateService>();
services.AddScoped<ISizeService, SizeService>();
services.AddScoped<IPreCheckService, PreCheckService>();
services.AddScoped<IDocumentTypeService, DocumentTypeService>();
services.AddScoped<ILogEntryService, LogEntryService>();
services.AddScoped<IUserUtilities, UserUtilities>();
services.AddScoped<IGeneralUtilities, GeneralUtilities>();
services.AddScoped<ICategoryCreateValidator, CategoryCreateValidator>();
services.AddScoped<ICategoryUpdateValidator, CategoryUpdateValidator>();
services.AddScoped<IItemCreateValidator, ItemCreateValidator>();
services.AddScoped<IItemUpdateValidator, ItemUpdateValidator>();
services.AddScoped<IItemTemplateCreateValidator, ItemTemplateCreateValidator>();
services.AddScoped<IItemTemplateUpdateValidator, ItemTemplateUpdateValidator>();
services.AddScoped<IListCreateValidator, ListCreateValidator>();
services.AddScoped<IListUpdateValidator, ListUpdateValidator>();
services.AddScoped<ILocationCreateValidator, LocationCreateValidator>();
services.AddScoped<ILocationUpdateValidator, LocationUpdateValidator>();
services.AddScoped<IVendorCreateValidator, VendorCreateValidator>();
services.AddScoped<IVendorUpdateValidator, VendorUpdateValidator>();
services.AddScoped<IPreCheckCreateValidator, PreCheckCreateValidator>();
services.AddScoped<IPreCheckUpdateValidator, PreCheckUpdateValidator>();
services.AddScoped<ISizeCreateValidator, SizeCreateValidator>();
services.AddScoped<ISizeUpdateValidator, SizeUpdateValidator>();
services.AddScoped<IDocumentTypeCreateValidator, DocumentTypeCreateValidator>();
services.AddScoped<IDocumentTypeUpdateValidator, DocumentTypeUpdateValidator>();
services.AddScoped<IDocumentUploadValidator, DocumentUploadValidator>();
// services.AddHostedService<UserCreateHandler>();
// services.AddHostedService<UserUpdateHandler>();
// services.AddHostedService<UserDeleteHandler>();
services.AddHostedService<ServiceBusChecklistTemplateProcessor>();
services.AddControllers();
// Add DbContext
var connectionString = GetSecretValueFromKeyVault(Configuration["AzureKeyVault:ConnectionStringSecretName"]);
services.AddDbContext<InventoryDbContext>(options => options.UseSqlServer(connectionString));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
services.AddEndpointsApiExplorer();
}
private void ConfigureAuthenticationAndAuthorization(IServiceCollection services)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(options =>
{
Configuration.Bind("AzureAd", options);
options.TokenValidationParameters.NameClaimType = "name";
}, options => { Configuration.Bind("AzureAd", options); });
services.AddAuthorizationBuilder()
.AddPolicy("AuthZPolicy", policyBuilder =>
policyBuilder.Requirements.Add(new ScopeAuthorizationRequirement { RequiredScopesConfigurationKey = "AzureAd.Scopes" }));
services.AddControllersWithViews()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, InventoryDbContext dbContext)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
// app.UseSwagger();
// app.UseSwaggerUI();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseSerilogRequestLogging();
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "swagger");
options.OAuthAppName("Swagger Client");
options.OAuthClientId("a053137b-4efd-46a8-bf41-7ed3fc49f3be");
options.OAuthClientSecret(Configuration["AzureAd:ClientSecret"]);
options.OAuthUseBasicAuthenticationWithAccessCodeGrant();
});
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", Serilog.Events.LogEventLevel.Warning)
.WriteTo.Console()
.WriteTo.File("logs/log-.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
dbContext.Database.Migrate();
app.UseHttpsRedirection();
app.UseRouting();
// Enable CORS
app.UseCors("AllowAllHeaders");
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
private string? GetSecretValueFromKeyVault(string? secretName)
{
var keyVaultUrl = Configuration["AzureKeyVault:VaultUrl"];
var credential = new DefaultAzureCredential();
if (keyVaultUrl == null) return null;
var client = new SecretClient(new Uri(keyVaultUrl), credential);
var secret = client.GetSecret(secretName);
return secret.Value.Value;
}
}