source: Farmatiko/Startup.cs@ e0cdea2

Last change on this file since e0cdea2 was e0cdea2, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 4 years ago

Fix all bugs

  • Property mode set to 100644
File size: 7.0 KB
RevLine 
[30a465f]1using Microsoft.AspNetCore.Builder;
2using Microsoft.AspNetCore.Hosting;
3using Microsoft.AspNetCore.SpaServices.AngularCli;
4using Microsoft.Extensions.Configuration;
5using Microsoft.Extensions.DependencyInjection;
6using Microsoft.Extensions.Hosting;
7using FarmatikoData;
8using Microsoft.EntityFrameworkCore;
[d2e69be]9using FarmatikoData.FarmatikoRepoInterfaces;
10using FarmatikoData.FarmatikoRepo;
[e42f61a]11using FarmatikoServices.FarmatikoServiceInterfaces;
12using FarmatikoServices.Services;
[c406ae5]13using Microsoft.Extensions.Logging;
[d23bf72]14using Microsoft.AspNetCore.Authentication.JwtBearer;
15using Microsoft.IdentityModel.Tokens;
16using System.Text;
17using FarmatikoServices.Auth;
18using FarmatikoServices.Infrastructure;
19using System;
[30a465f]20namespace Farmatiko
21{
22 public class Startup
23 {
[e42f61a]24 readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
25
[30a465f]26 public Startup(IConfiguration configuration)
27 {
28 Configuration = configuration;
29 }
30
31 public IConfiguration Configuration { get; }
32
33 // This method gets called by the runtime. Use this method to add services to the container.
34 public void ConfigureServices(IServiceCollection services)
35 {
[e42f61a]36 services.AddCors(options =>
37 {
38 options.AddPolicy(name: MyAllowSpecificOrigins,
39 builder =>
40 {
41 builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin();
42 });
43 });
44
[30a465f]45 services.AddControllersWithViews();
[1db5673]46 services.AddControllersWithViews().AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
[30a465f]47 // In production, the Angular files will be served from this directory
48 services.AddSpaStaticFiles(configuration =>
49 {
50 configuration.RootPath = "ClientApp/dist";
51 });
52 var connectionString = Configuration.GetSection("ConnectionStrings").GetValue<string>("FarmatikoConnection");
53 services.AddEntityFrameworkNpgsql().AddDbContext<FarmatikoDataContext>(opt => opt.UseNpgsql(connectionString));
54
[1db5673]55 services.AddScoped<IPHRepo, PHRepo>();
[e0cdea2]56 services.AddScoped<IRepository, Repository>();
57 services.AddScoped<IAdminRepo, AdminRepo>();
[4e72684]58
[e0cdea2]59 services.AddScoped<IPHService, PHService>();
[6f203af]60 services.AddTransient<IAdminService, AdminService>();
61 services.AddTransient<IService, Service>();
[a55ef91]62
63 services.AddTransient<IProcessJSONService, ProcessJSONService>();
[c406ae5]64
65 services.AddTransient<ILogger, Logger<ProcessJSONService>>();
[d23bf72]66
67 // services.AddTransient<ISystemService, SystemService>();
68
69
70 var jwtTokenConfig = Configuration.GetSection("jwtTokenConfig").Get<JwtTokenConfig>();
71 services.AddSingleton(jwtTokenConfig);
72
73 services.AddAuthentication(o =>
74 {
75 o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
76 o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
77 o.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
78 }).AddJwtBearer(x =>
79 {
80 x.RequireHttpsMetadata = true;
81 x.SaveToken = true;
82 x.TokenValidationParameters = new TokenValidationParameters
83 {
84 ValidateIssuer = true,
85 ValidIssuer = jwtTokenConfig.Issuer,
86 ValidateIssuerSigningKey = true,
87 IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtTokenConfig.Secret)),
88 ValidAudience = jwtTokenConfig.Audience,
89 ValidateAudience = true,
90 ValidateLifetime = true,
91 ClockSkew = TimeSpan.FromMinutes(1)
92 };
93 });
94
95 /*.AddJwtBearer(cfg =>
96 {
97 cfg.RequireHttpsMetadata = false;
98 cfg.SaveToken = true;
99 cfg.IncludeErrorDetails = true;
100 cfg.TokenValidationParameters = new TokenValidationParameters()
101 {
102 ValidIssuer = Configuration.GetSection("TokenIssuer").Value,
103 ValidAudience = Configuration.GetSection("TokenIssuer").Value,
104 IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration.GetSection("SecretKey").Value))
105 };
106
107 });
108*/
[7520f88]109 services.AddSingleton<IJwtAuthManager>(new JwtAuthManager(jwtTokenConfig));
[d23bf72]110 services.AddHostedService<JwtRefreshTokenCache>();
111 services.AddScoped<IAuthService, AuthService>();
112 //If we add imgs
113 /*services.Configure<FormOptions>(o => {
114 o.ValueLengthLimit = int.MaxValue;
115 o.MultipartBodyLengthLimit = int.MaxValue;
116 o.MemoryBufferThreshold = int.MaxValue;
117 });*/
118
[30a465f]119 }
120
121 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
122 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
123 {
124 if (env.IsDevelopment())
125 {
126 app.UseDeveloperExceptionPage();
127 }
128 else
129 {
130 app.UseExceptionHandler("/Error");
131 // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
132 app.UseHsts();
133 }
[d23bf72]134 app.UseExceptionHandler("/Error");
[30a465f]135 app.UseHttpsRedirection();
136 app.UseStaticFiles();
[d23bf72]137
138 // if we add imgs
139 /*app.UseStaticFiles(new StaticFileOptions()
140 {
141 FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot")),
142 RequestPath = new PathString("/wwwroot")
143 });*/
144
[30a465f]145 if (!env.IsDevelopment())
146 {
147 app.UseSpaStaticFiles();
148 }
149
150 app.UseRouting();
151
[d23bf72]152 app.UseAuthentication();
153 app.UseAuthorization();
154
[e42f61a]155 app.UseCors(MyAllowSpecificOrigins);
156
[30a465f]157 app.UseEndpoints(endpoints =>
158 {
159 endpoints.MapControllerRoute(
160 name: "default",
[d23bf72]161 pattern: "api/{controller}/{action=Index}/{id?}");
[30a465f]162 });
163
164 app.UseSpa(spa =>
165 {
166 // To learn more about options for serving an Angular SPA from ASP.NET Core,
167 // see https://go.microsoft.com/fwlink/?linkid=864501
168
169 spa.Options.SourcePath = "ClientApp";
170
171 if (env.IsDevelopment())
172 {
173 spa.UseAngularCliServer(npmScript: "start");
174 }
175 });
176 }
177 }
178}
Note: See TracBrowser for help on using the repository browser.