-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
142 lines (115 loc) · 4.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
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using ticketsystem_backend.Data;
using ticketsystem_backend.Migrations;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using Microsoft.AspNetCore.Http;
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.AddCors();
services.AddControllers();
services.AddCors(options =>
{
options.AddPolicy(name: MySpecificOrigin, builder =>
{
builder
//.WithOrigins("https://epic-northcutt-0cee3d.netlify.app", "https://www.hetfeld.name", "http://localhost")
//.WithOrigins("https://epic-northcutt-0cee3d.netlify.app", "http://localhost:3000")
.AllowAnyOrigin()
//.SetIsOriginAllowed(origin => true)
//.WithHeaders("Content-Type", "application/json")
//.WithMethods("PUT", "DELETE", "GET", "POST", "OPTIONS")
//.AllowCredentials()
.AllowAnyHeader()
.AllowAnyMethod()
;
});
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ticketsystem_backend", Version = "v1" });
});
services.AddDbContext<TicketSystemDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("TicketContext")));
services.AddTransient<DbSeedData>();
services.AddAuthentication(opt =>
{
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
//ValidIssuer = "https://epic-northcutt-0cee3d.netlify.app",
ValidIssuer = "*",
ValidAudience = "*",
//ValidAudience = "https://epic-northcutt-0cee3d.netlify.app",
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();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ticketsystem_backend v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(MySpecificOrigin);
//app.UseCors(x => x
// .AllowAnyMethod()
// .AllowAnyHeader()
// .SetIsOriginAllowed(origin => true)
// .AllowCredentials());
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
//endpoints.MapGet("/echo",
//context => context.Response.WriteAsync("echo"))
//.RequireCors(MySpecificOrigin);
//endpoints.MapControllers()
// .RequireCors(MySpecificOrigin);
//endpoints.MapGet("/echo2",
// context => context.Response.WriteAsync("echo2"));
//endpoints.MapRazorPages();
});
seeder.EnsureSeedData();
}
}
}