123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using EasyMemoryCache;
- using Microsoft.AspNetCore.Authentication.JwtBearer;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
- using Microsoft.Extensions.Logging;
- using Microsoft.OpenApi.Models;
- using Quartz;
- using Quartz.Impl;
- using Sugar.Enties;
- using System;
- using System.IO;
- using System.Reflection;
- using Utils;
- using Utils.Jwt;
- namespace PMS.NetCore
- {
- public class Startup
- {
- private IWebHostEnvironment _env;
- private const string CONTENTROOTPATHTOKEN = "%CONTENTROOTPATH%";
- public Startup(IConfiguration configuration, IWebHostEnvironment env)
- {
- Configuration = configuration;
- _env = env;
- }
- 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)
- {
- var signingConfigurations = new SigningConfigurations();
- services.AddSingleton(signingConfigurations);
- services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
- // TODO create options classes for app settings
- var identityConn = AppSettingsHelper.Configuration["AppSettings:ConnectionString"];
- //identityConn = identityConn.Replace(CONTENTROOTPATHTOKEN, _env.ContentRootPath);
- services.AddDbContext<IdentityAuthDbContext>(options => options.UseSqlServer(identityConn));
- services.AddDefaultIdentity<AppUser>().AddEntityFrameworkStores<IdentityAuthDbContext>();
- services.AddMemoryCache();
- #region jwt配置
- services.AddTransient<ITokenHelper, TokenHelper>();
- //读取配置文件配置的jwt相关配置
- services.Configure<JWTConfig>(Configuration.GetSection("JWTConfig"));
- //启用JWT
- services.AddAuthentication(Options =>
- {
- Options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
- Options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
- }).
- AddJwtBearer();
- services.AddScoped<Filter.TokenAuthorize>();
- #endregion
- services.AddLogging(log =>
- {
- log.AddConsole();
- log.AddDebug();
- });
- services.AddAuthorization(auth =>
- {
- auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
- .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
- .RequireAuthenticatedUser().Build());
- });
- services.AddMvcCore()
- .AddApiExplorer();
- services.AddHttpClient();
- services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
- {
- builder.AllowAnyOrigin()
- .AllowAnyMethod()
- .AllowAnyHeader();
- }));
- services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
- services.AddSingleton<IConfiguration>(Configuration);
- services.AddSingleton<ICaching, Caching>();
- services.AddSwaggerGen(c =>
- {
- c.SwaggerDoc("v1", new OpenApiInfo { Title = "PMS.NetCore API——Net5.0", Version = "v1" });
- // 为 Swagger 设置xml文档注释路径
- var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
- var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
- // 添加控制器层注释,true表示显示控制器注释
- c.IncludeXmlComments(xmlPath, true);
- });
- services.AddControllers();
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
- {
- var isIIS = Convert.ToBoolean(AppSettingsHelper.Configuration["isIIS"]);
- var virtualPath = AppSettingsHelper.Configuration["virtualPath"];
- var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- app.UseHttpsRedirection();
- app.UseRouting();
- app.UseAuthorization();
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllers();
- });
- app.UseSwagger();
- if (isIIS == false) //若不是IIS部署
- {
- logger.Info($"isIIS:{isIIS}");
- app.UseSwaggerUI(c =>
- {
- c.SwaggerEndpoint("/swagger/v1/swagger.json", "PMS.NetCore v1");
- });
- }
- else //若是IIS部署,并且是虚拟目录部署
- {
- logger.Info($"isIIS:{isIIS}");
- app.UseSwaggerUI(c =>
- {
- c.SwaggerEndpoint($"{virtualPath}/swagger/v1/swagger.json", "PMS.NetCore v1");
- });
- }
- }
- }
- }
|