using Autofac; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.Filters; using System; using System.IO; using System.Reflection; using Wms_09.AutoMapperServer; using Wms_09.Filter; using Wms_09.Tools; using WMS.Entity.Context; namespace Wms_09 { 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() .AddJsonOptions(options => { //²»Ê¹ÓÃÍÕ·åÑùʽµÄkey options.JsonSerializerOptions.PropertyNamingPolicy = null; } ); //È«¾ÖÅäÖÃJsonÐòÁл¯´¦Àí services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "WMSºǫ́¹ÜÀí", Version = "v1" }); #region ¶ÁÈ¡xmlÐÅÏ¢ var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename)); #endregion var openApiSecurity = new OpenApiSecurityScheme { Description = "JWTÈÏÖ¤ÊÚȨ£¬Ê¹ÓÃÖ±½ÓÔÚÏ¿òÖÐÊäÈëBearer {token}£¨×¢ÒâÁ½ÕßÖ®¼äÊÇÒ»¸ö¿Õ¸ñ£©\"", Name = "token", //jwt ĬÈϲÎÊýÃû³Æ In = ParameterLocation.Header, //jwtĬÈÏ´æ·ÅAuthorizationÐÅÏ¢µÄλÖã¨ÇëÇóÍ·£© Type = SecuritySchemeType.ApiKey }; c.AddSecurityDefinition("oauth2", openApiSecurity); c.OperationFilter(); }); #region ¿çÓò´¦Àí services.AddCors(options => { options.AddPolicy(name: "MyCors", builder => { builder.AllowAnyMethod() .SetIsOriginAllowed(_ => true) .AllowAnyHeader() .AllowCredentials(); }); }); #endregion #region jwtÅäÖà services.AddTransient(); //¶ÁÈ¡ÅäÖÃÎļþÅäÖõÄjwtÏà¹ØÅäÖà services.Configure(Configuration.GetSection("JWTConfig")); //ÆôÓÃJWT services.AddAuthentication(Options => { Options.AddScheme(MyAuthHandler.SchemeName, "default scheme"); Options.DefaultAuthenticateScheme = MyAuthHandler.SchemeName; Options.DefaultChallengeScheme = MyAuthHandler.SchemeName; }).AddJwtBearer(); #endregion //¶ÁÈ¡ÅäÖÃÎļþÅäÖõĽӿڽ»»¥µÄÏà¹ØÅäÖà services.Configure(Configuration.GetSection("ApiUrlConfig")); //Êý¾Ý¿âÅäÖà BaseDbConfig.ConnectionString = Configuration.GetSection("AppSettings:ConnectionString").Value; #region AutoMapper×¢Èë services.AddAutoMapper(typeof(AutoMapperProfile)); // automapperÒÀÀµ #endregion } public void ConfigureContainer(ContainerBuilder builder) { builder.RegisterAssemblyTypes(Assembly.Load("WMS.IDAL"), Assembly.Load("WMS.DAL"), Assembly.Load("WMS.IBLL"), Assembly.Load("WMS.BLL")) .InstancePerLifetimeScope() .AsImplementedInterfaces(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApiDemo v1"); c.RoutePrefix = string.Empty; }); } //app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseCors("MyCors");//¿çÓò app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }