source: Farmatiko/Startup.cs@ d23bf72

Last change on this file since d23bf72 was d23bf72, checked in by DimitarSlezenkovski <dslezenkovski@…>, 3 years ago

Add SystemService, Auth, fix a lil bugs :)

  • Property mode set to 100644
File size: 6.8 KB
Line 
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;
9using FarmatikoData.FarmatikoRepoInterfaces;
10using FarmatikoData.FarmatikoRepo;
11using FarmatikoServices.FarmatikoServiceInterfaces;
12using FarmatikoServices.Services;
13using Microsoft.Extensions.Logging;
14using Microsoft.AspNetCore.Authentication.JwtBearer;
15using Microsoft.IdentityModel.Tokens;
16using System.Text;
17using FarmatikoServices.Auth;
18using FarmatikoServices.Infrastructure;
19using System;
20
21namespace Farmatiko
22{
23 public class Startup
24 {
25 readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
26
27 public Startup(IConfiguration configuration)
28 {
29 Configuration = configuration;
30 }
31
32 public IConfiguration Configuration { get; }
33
34 // This method gets called by the runtime. Use this method to add services to the container.
35 public void ConfigureServices(IServiceCollection services)
36 {
37 services.AddCors(options =>
38 {
39 options.AddPolicy(name: MyAllowSpecificOrigins,
40 builder =>
41 {
42 builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin();
43 });
44 });
45
46 services.AddControllersWithViews();
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
55 services.AddTransient<IPHRepo, PHRepo>();
56 services.AddTransient<IRepository, Repository>();
57 services.AddTransient<IAdminRepo, AdminRepo>();
58
59 services.AddTransient<IPHService, PHService>();
60 services.AddTransient<IAdminService, AdminService>();
61 services.AddTransient<IService, Service>();
62
63 services.AddTransient<IProcessJSONService, ProcessJSONService>();
64
65 services.AddTransient<ILogger, Logger<ProcessJSONService>>();
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*/
109 services.AddSingleton<IJwtAuthManager, JwtAuthManager>();
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
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 }
134 app.UseExceptionHandler("/Error");
135 app.UseHttpsRedirection();
136 app.UseStaticFiles();
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
145 if (!env.IsDevelopment())
146 {
147 app.UseSpaStaticFiles();
148 }
149
150 app.UseRouting();
151
152 app.UseAuthentication();
153 app.UseAuthorization();
154
155 app.UseCors(MyAllowSpecificOrigins);
156
157 app.UseEndpoints(endpoints =>
158 {
159 endpoints.MapControllerRoute(
160 name: "default",
161 pattern: "api/{controller}/{action=Index}/{id?}");
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.