Startup.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.AddDbContext<spDbContext>(optionsAction =>
  59. optionsAction.UseSqlServer(Configuration.GetConnectionString("DefaultConnect"))
  60. );
  61. services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
  62. }
  63. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  64. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  65. {
  66. if (env.IsDevelopment())
  67. {
  68. app.UseDeveloperExceptionPage();
  69. }
  70. app.UseCors(MyAllowSpecificOrigins);
  71. app.UseHttpsRedirection();
  72. app.UseRouting();
  73. app.UseAuthentication();
  74. app.UseAuthorization();
  75. app.UseEndpoints(endpoints =>
  76. {
  77. endpoints.MapControllers();
  78. });
  79. }
  80. }
  81. }