Startup.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Microsoft.AspNetCore.Authentication.JwtBearer;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.EntityFrameworkCore;
  5. using Microsoft.Extensions.Configuration;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Hosting;
  8. using Microsoft.IdentityModel.Tokens;
  9. using Newtonsoft.Json;
  10. using Quartz;
  11. using Quartz.Impl;
  12. using System;
  13. using System.Text;
  14. using System.Text.Json.Serialization;
  15. using wispro.sp.api.Job;
  16. namespace wispro.sp.api
  17. {
  18. public class Startup
  19. {
  20. readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
  21. public Startup(IConfiguration configuration)
  22. {
  23. Configuration = configuration;
  24. }
  25. public IConfiguration Configuration { get; }
  26. // This method gets called by the runtime. Use this method to add services to the container.
  27. public void ConfigureServices(IServiceCollection services)
  28. {
  29. services.AddCors(options =>
  30. {
  31. options.AddPolicy(MyAllowSpecificOrigins,
  32. builder =>
  33. {
  34. builder.AllowAnyOrigin()
  35. .AllowAnyMethod()
  36. .AllowAnyHeader();
  37. });
  38. });
  39. services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(option =>
  40. {
  41. option.TokenValidationParameters = new TokenValidationParameters
  42. {
  43. ValidateIssuer = false,
  44. ValidateAudience = false,
  45. ValidateLifetime = true,
  46. ValidateIssuerSigningKey = true,
  47. ValidIssuer = Configuration["jwt:Issuer"],
  48. IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["jwt:Key"])),
  49. ClockSkew = TimeSpan.Zero
  50. };
  51. });
  52. services.AddControllers().AddNewtonsoftJson(o =>
  53. {
  54. o.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
  55. o.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;
  56. o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
  57. });
  58. //services.AddControllers().AddJsonOptions(options =>
  59. //{
  60. // options.JsonSerializerOptions.IgnoreNullValues = true;
  61. // options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
  62. //});
  63. services.AddDbContext<spDbContext>(optionsAction =>
  64. optionsAction.UseSqlServer(Configuration.GetConnectionString("DefaultConnect"))
  65. );
  66. services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
  67. }
  68. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  69. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  70. {
  71. if (env.IsDevelopment())
  72. {
  73. app.UseDeveloperExceptionPage();
  74. }
  75. app.UseCors(MyAllowSpecificOrigins);
  76. app.UseHttpsRedirection();
  77. app.UseRouting();
  78. app.UseAuthentication();
  79. app.UseAuthorization();
  80. app.UseEndpoints(endpoints =>
  81. {
  82. endpoints.MapControllers();
  83. });
  84. //JobKey jobKey = new JobKey("ImportReportData");
  85. //var trigger = TriggerBuilder.Create()
  86. // .WithDescription("µ¼ÈëÿÔ±¨±í")
  87. // .WithSchedule(CronScheduleBuilder.CronSchedule("0 0/2 * * * ? *").WithMisfireHandlingInstructionDoNothing())
  88. // .Build();
  89. //QuartzUtil.Add(typeof(ImportReportJob), jobKey, trigger);
  90. }
  91. }
  92. }