-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
99 lines (85 loc) · 3.39 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
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using ticketsystem_backend.Data;
using ticketsystem_backend.Migrations;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
namespace ticketsystem_backend
{
public class Startup
{
readonly string MySpecificOrigin = "EnableCORS";
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.AddControllers();
// add cors to service and configure it
services.AddCors(options =>
{
options.AddPolicy(name: MySpecificOrigin, builder =>
{
// no restrictions due to prototype use
builder
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
// add database context to service
services.AddDbContext<TicketSystemDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("TicketContext")));
services.AddTransient<DbSeedData>();
// add authentication to service using JwtBearer
services.AddAuthentication(opt =>
{
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
// configure JwtBearer
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = "*",
ValidAudience = "*",
IssuerSigningKey = new SymmetricSecurityKey(Base64UrlEncoder.DecodeBytes("MBcCT4UEs67vh3shK683Lxhn33t2LTtH"))
};
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DbSeedData seeder)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// use https connection
app.UseHttpsRedirection();
app.UseRouting();
// use cors that is configured in ConfigureServices
app.UseCors(MySpecificOrigin);
// use authentication that is configured in ConfigureServices
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
// seed data for development use
seeder.EnsureSeedData();
}
}
}