Files
pecklist/Startup.cs

227 lines
8.6 KiB
C#

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using GZTW.Pecklist.Models;
using GZTW.Pecklist.Util;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
namespace GZTW.Pecklist
{
public class Startup
{
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().AddNewtonsoftJson();
services.AddDbContext<PecklistContext>(options =>
{
options.UseSqlite(Configuration.GetConnectionString("thedb")).EnableSensitiveDataLogging(false);
});
var secretKey = Configuration.GetSection("JWT").GetValue<string>("secret");
var signingKey = new SymmetricSecurityKey(System.Text.Encoding.ASCII.GetBytes(secretKey));
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
// Token signature will be verified using a private key.
ValidateIssuerSigningKey = true,
RequireSignedTokens = true,
IssuerSigningKey = signingKey,
ValidateIssuer = true,
ValidIssuer = "GZTW_Pecklist",
ValidateAudience = false,
//Note: these are all enabled in AyaNOva but were origionally disabled in rf
// // Token will only be valid if not expired yet, with 5 minutes clock skew.
// ValidateLifetime = true,
// RequireExpirationTime = true,
// ClockSkew = new TimeSpan(0, 5, 0),
};
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, PecklistContext dbContext)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseDefaultFiles();
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = context =>
{
if (context.File.Name == "default.htm")
{
context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store");
context.Context.Response.Headers.Add("Expires", "-1");
}
}
});
app.UseAuthentication();
//Check schema
Schema.CheckAndUpdate(dbContext);
// app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
//server ready
System.Diagnostics.Debug.WriteLine("BOOT COMPLETED - OPEN");
}
}
}
// using Microsoft.AspNetCore.Builder;
// using Microsoft.IdentityModel.Tokens;
// using Microsoft.AspNetCore.HttpOverrides;
// using Microsoft.Extensions.Configuration;
// using Microsoft.Extensions.DependencyInjection;
// using Microsoft.Extensions.Logging;
// using Microsoft.EntityFrameworkCore;
// using Microsoft.Extensions.Hosting;
// using Microsoft.AspNetCore.Authentication.JwtBearer;
// using GZTW.Pecklist.Models;
// using GZTW.Pecklist.Util;
// namespace GZTW.Pecklist
// {
// public class Startup
// {
// public Startup(Microsoft.AspNetCore.Hosting.IWebHostEnvironment env)
// {
// var builder = new ConfigurationBuilder()
// .SetBasePath(env.ContentRootPath)
// .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
// .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
// .AddEnvironmentVariables();
// Configuration = builder.Build();
// }
// public IConfigurationRoot Configuration { get; }
// // This method gets called by the runtime. Use this method to add services to the container.
// public void ConfigureServices(IServiceCollection services)
// {
// services.AddDbContext<PecklistContext>(options => options.UseSqlite(Configuration.GetConnectionString("thedb")));
// //Added this so that can access configuration from anywhere else
// //See authcontroller for usage
// services.AddSingleton<IConfiguration>(Configuration);
// services.AddMvc();
// //get the key from the appsettings.json file
// var secretKey = Configuration.GetSection("JWT").GetValue<string>("secret");
// var signingKey = new SymmetricSecurityKey(System.Text.Encoding.ASCII.GetBytes(secretKey));
// services.AddAuthentication(options =>
// {
// options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
// options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
// }).AddJwtBearer(options =>
// {
// // options.AutomaticAuthenticate = true;
// // options.AutomaticChallenge = true;
// options.TokenValidationParameters = new TokenValidationParameters
// {
// // Token signature will be verified using a private key.
// ValidateIssuerSigningKey = true,
// RequireSignedTokens = true,
// IssuerSigningKey = signingKey,
// ValidateIssuer = true,
// ValidIssuer = "GZTW_Pecklist",
// ValidateAudience = false,
// // ValidAudience = "https://yourapplication.example.com",
// // Token will only be valid if not expired yet, with 5 minutes clock skew.
// // ValidateLifetime = true,
// // RequireExpirationTime = true,
// // ClockSkew = new TimeSpan(0, 5, 0),
// };
// });
// }
// public void Configure(IApplicationBuilder app, IHostEnvironment env, PecklistContext dbContext)
// {//ILoggerFactory loggerFactory,
// if (env.IsDevelopment())
// {
// app.UseDeveloperExceptionPage();
// }
// var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
// // loggerFactory.AddConsole(Configuration.GetSection("Logging"));
// // loggerFactory.AddDebug();
// app.UseForwardedHeaders(new ForwardedHeadersOptions
// {
// ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
// });
// app.UseDefaultFiles();
// app.UseStaticFiles(new StaticFileOptions
// {
// OnPrepareResponse = context =>
// {
// if (context.File.Name == "index.html" || context.File.Name == "sw.js")
// {
// context.Context.Response.Headers.Add("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0");
// // context.Context.Response.Headers.Add("Expires", "-1");
// }
// }
// });
// app.UseAuthentication();
// //app.UseMvc();
// //Check schema
// Schema.CheckAndUpdate(dbContext);
// }
// }
// }