From 4e13b3c9bb34b798cc2bb6915790755257f3308d Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 8 Jun 2020 22:05:40 +0000 Subject: [PATCH] Migrated to latest libs and .net core 3.1 fingers crossed... --- Program.cs | 50 +++++++-- Startup.cs | 257 ++++++++++++++++++++++++++++++++------------ rockfishCore.csproj | 15 +-- util/RfMail.cs | 14 +-- 4 files changed, 247 insertions(+), 89 deletions(-) diff --git a/Program.cs b/Program.cs index 7b5c39d..82ffc88 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,11 @@ -using Microsoft.AspNetCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; namespace rockfishCore { @@ -7,17 +13,45 @@ namespace rockfishCore { public static void Main(string[] args) { - BuildWebHost(args).Run(); + CreateHostBuilder(args).Build().Run(); } - public static IWebHost BuildWebHost(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseUrls("http://*:3001") + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup() + .UseUrls("http://*:3001") .CaptureStartupErrors(true) .UseSetting("detailedErrors", "true") .UseKestrel() - .UseContentRoot(System.IO.Directory.GetCurrentDirectory()) - .UseStartup() - .Build(); + .UseContentRoot(System.IO.Directory.GetCurrentDirectory()); + }); } } + + + + +// namespace rockfishCore +// { +// public class Program +// { +// public static void Main(string[] args) +// { +// BuildWebHost(args).Run(); +// } + +// public static IWebHost BuildWebHost(string[] args) => +// WebHost.CreateDefaultBuilder(args) +// .UseUrls("http://*:3001") +// .CaptureStartupErrors(true) +// .UseSetting("detailedErrors", "true") +// .UseKestrel() +// .UseContentRoot(System.IO.Directory.GetCurrentDirectory()) +// .UseStartup() +// .Build(); +// } +// } + + diff --git a/Startup.cs b/Startup.cs index 700a98e..1bc25b2 100644 --- a/Startup.cs +++ b/Startup.cs @@ -4,92 +4,90 @@ using System.Linq; using System.Threading.Tasks; 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; -//manually added -using Microsoft.EntityFrameworkCore; using rockfishCore.Models; +using rockfishCore.Util; +using Microsoft.EntityFrameworkCore; +using System.IO; +using System.Reflection; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.IdentityModel.Tokens; -using rockfishCore.Util; -//added when upgrade to v2 of .netcore -using Microsoft.AspNetCore.Authentication; -//this comment added in windows with notepad++ -//this comment added in Linux with vscode + + +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.ApiExplorer; +using Microsoft.Extensions.Options; + + + + namespace rockfishCore { public class Startup { - public Startup(IHostingEnvironment env) + public Startup(IConfiguration configuration) { - 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(); + Configuration = configuration; } - public IConfigurationRoot Configuration { get; } + 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(); + services.AddDbContext(options => + { + options.UseSqlite(Configuration.GetConnectionString("rfdb")).EnableSensitiveDataLogging(false); - services.AddDbContext( - options => - { - options.UseSqlite(Configuration.GetConnectionString("rfdb")); - options.EnableSensitiveDataLogging(false); - } + }); - ); - //Added this so that can access configuration from anywhere else - //See authcontroller for usage - services.AddSingleton(Configuration); - - services.AddMvc(); - - //get the key from the appsettings.json file var secretKey = Configuration.GetSection("JWT").GetValue("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 = "rockfishCore", - ValidateAudience = false, - // ValidAudience = "https://yourapplication.example.com", + { + 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 = "rockfishCore", + 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), + }; + }); + - // 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, IHostingEnvironment env, ILoggerFactory loggerFactory, rockfishContext dbContext) + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, rockfishContext dbContext) { - loggerFactory.AddConsole(Configuration.GetSection("Logging")); - loggerFactory.AddDebug(); + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + app.UseDefaultFiles(); app.UseStaticFiles(new StaticFileOptions { @@ -103,20 +101,145 @@ namespace rockfishCore } }); app.UseAuthentication(); - app.UseMvc(); //Check schema RfSchema.CheckAndUpdate(dbContext); - //bool bMM=RfMail.MailIsMirroringProperly(); - // try - // { - // var test = OpsDiagnostics.VerifyBackups(); - // } - // catch (Exception ex) - // { - // string res = ex.Message; - // } + app.UseHttpsRedirection(); - }//eof + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } } } + + + + +// using System; +// using System.Collections.Generic; +// using System.Linq; +// using System.Threading.Tasks; +// using Microsoft.AspNetCore.Builder; +// using Microsoft.AspNetCore.Hosting; +// using Microsoft.Extensions.Configuration; +// using Microsoft.Extensions.DependencyInjection; +// using Microsoft.Extensions.Logging; + +// //manually added +// using Microsoft.EntityFrameworkCore; +// using rockfishCore.Models; +// using Microsoft.AspNetCore.Authentication.JwtBearer; +// using Microsoft.IdentityModel.Tokens; +// using rockfishCore.Util; + +// //added when upgrade to v2 of .netcore +// using Microsoft.AspNetCore.Authentication; +// //this comment added in windows with notepad++ +// //this comment added in Linux with vscode +// namespace rockfishCore +// { +// public class Startup +// { +// public Startup(IHostingEnvironment 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( +// options => +// { +// options.UseSqlite(Configuration.GetConnectionString("rfdb")); +// options.EnableSensitiveDataLogging(false); +// } + +// ); + +// //Added this so that can access configuration from anywhere else +// //See authcontroller for usage +// services.AddSingleton(Configuration); + +// services.AddMvc(); + +// //get the key from the appsettings.json file +// var secretKey = Configuration.GetSection("JWT").GetValue("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 = "rockfishCore", +// 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, IHostingEnvironment env, ILoggerFactory loggerFactory, rockfishContext dbContext) +// { +// loggerFactory.AddConsole(Configuration.GetSection("Logging")); +// loggerFactory.AddDebug(); +// 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(); +// app.UseMvc(); +// //Check schema +// RfSchema.CheckAndUpdate(dbContext); + +// //bool bMM=RfMail.MailIsMirroringProperly(); +// // try +// // { +// // var test = OpsDiagnostics.VerifyBackups(); +// // } +// // catch (Exception ex) +// // { +// // string res = ex.Message; +// // } + +// }//eof +// } +// } diff --git a/rockfishCore.csproj b/rockfishCore.csproj index e29a38d..eb5bc72 100644 --- a/rockfishCore.csproj +++ b/rockfishCore.csproj @@ -1,6 +1,6 @@  - netcoreapp2.1 + netcoreapp3.1 @@ -8,12 +8,13 @@ - - - - - - + + + + + + + \ No newline at end of file diff --git a/util/RfMail.cs b/util/RfMail.cs index b976c40..3fb1e08 100644 --- a/util/RfMail.cs +++ b/util/RfMail.cs @@ -119,8 +119,8 @@ namespace rockfishCore.Util bool trackDeliveryStatus = false, string CustomHeader = "", string CustomHeaderValue = "") { var message = new MimeMessage(); - message.From.Add(new MailboxAddress(MessageFrom)); - message.To.Add(new MailboxAddress(MessageTo)); + message.From.Add(new MailboxAddress("",MessageFrom)); + message.To.Add(new MailboxAddress("",MessageTo)); message.Subject = MessageSubject; message.Body = new TextPart("plain") @@ -273,12 +273,12 @@ namespace rockfishCore.Util case rfMailAccount.sales: from_account = MAIL_ACCOUNT_SALES; from_account_password = MAIL_ACCOUNT_PASSWORD_SALES; - from = new MailboxAddress(from_account); + from = new MailboxAddress("",from_account); break; default: from_account = MAIL_ACCOUNT_SUPPORT; from_account_password = MAIL_ACCOUNT_PASSWORD_SUPPORT; - from = new MailboxAddress(from_account); + from = new MailboxAddress("",from_account); break; } reply.From.Add(from); @@ -520,7 +520,7 @@ namespace rockfishCore.Util string CustomHeader = "", string CustomHeaderValue = "") { var message = new MimeMessage(); - message.From.Add(new MailboxAddress(MessageFrom)); + message.From.Add(new MailboxAddress("",MessageFrom)); //case 3512 handle more than one email in the address @@ -530,13 +530,13 @@ namespace rockfishCore.Util var addrs = MessageTo.Split(','); foreach (string addr in addrs) { - mbAll.Add(new MailboxAddress(addr.Trim())); + mbAll.Add(new MailboxAddress("",addr.Trim())); } message.To.AddRange(mbAll); } else { - message.To.Add(new MailboxAddress(MessageTo)); + message.To.Add(new MailboxAddress("",MessageTo)); }