Startup.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 System;
  10. using System.Text;
  11. namespace wispro.sp.api
  12. {
  13. public class Startup
  14. {
  15. readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
  16. public Startup(IConfiguration configuration)
  17. {
  18. Configuration = configuration;
  19. }
  20. public IConfiguration Configuration { get; }
  21. // This method gets called by the runtime. Use this method to add services to the container.
  22. public void ConfigureServices(IServiceCollection services)
  23. {
  24. services.AddCors(options =>
  25. {
  26. options.AddPolicy(MyAllowSpecificOrigins,
  27. builder =>
  28. {
  29. builder.AllowAnyOrigin()
  30. .AllowAnyMethod()
  31. .AllowAnyHeader();
  32. });
  33. });
  34. //services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
  35. // .AddJwtBearer(options =>
  36. // {
  37. // options.TokenValidationParameters = new TokenValidationParameters
  38. // {
  39. // ValidateIssuer = true,//是否验证Issuer
  40. // ValidateAudience = true,//是否验证Audience
  41. // ValidateLifetime = true,//是否验证失效时间
  42. // ValidateIssuerSigningKey = true,//是否验证SecurityKey
  43. // ValidAudience = Configuration["jwt:Audience"],//Audience
  44. // ValidIssuer = Configuration["jwt:Issuer"],//Issuer,这两项和签发jwt的设置一致
  45. // IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["jwt:Key"]))//拿到SecurityKey
  46. // };
  47. // });
  48. services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(option =>
  49. {
  50. option.TokenValidationParameters = new TokenValidationParameters
  51. {
  52. ValidateIssuer = false,
  53. ValidateAudience = false,
  54. ValidateLifetime = true,
  55. ValidateIssuerSigningKey = true,
  56. ValidIssuer = Configuration["jwt:Issuer"],
  57. IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["jwt:Key"])),
  58. ClockSkew = TimeSpan.Zero
  59. };
  60. });
  61. services.AddControllers().AddNewtonsoftJson(o=>o.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
  62. services.AddDbContext<spDbContext>(optionsAction =>
  63. optionsAction.UseSqlServer(Configuration.GetConnectionString("DefaultConnect"))
  64. );
  65. }
  66. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  67. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  68. {
  69. if (env.IsDevelopment())
  70. {
  71. app.UseDeveloperExceptionPage();
  72. }
  73. app.UseCors(MyAllowSpecificOrigins);
  74. app.UseHttpsRedirection();
  75. app.UseRouting();
  76. app.UseAuthentication();
  77. app.UseAuthorization();
  78. app.UseEndpoints(endpoints =>
  79. {
  80. endpoints.MapControllers();
  81. });
  82. }
  83. }
  84. }