|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using Autofac;
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using Microsoft.Extensions.FileProviders;
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
using Newtonsoft.Json;
|
|
|
using VOL.Core.Configuration;
|
|
|
using VOL.Core.Extensions;
|
|
|
using VOL.Core.Filters;
|
|
|
using VOL.Core.Middleware;
|
|
|
using VOL.Core.ObjectActionValidator;
|
|
|
|
|
|
namespace VOL.WebApi
|
|
|
{
|
|
|
public class Startup
|
|
|
{
|
|
|
public Startup(IConfiguration configuration)
|
|
|
{
|
|
|
Configuration = configuration;
|
|
|
}
|
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
private IServiceCollection Services { get; set; }
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
{
|
|
|
//³õʼ»¯Ä£ÐÍÑéÖ¤ÅäÖÃ
|
|
|
services.UseMethodsModelParameters().UseMethodsGeneralParameters();
|
|
|
services.AddSingleton<IObjectModelValidator>(new NullObjectModelValidator());
|
|
|
Services = services;
|
|
|
// services.Replace( ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
|
|
services.AddSession();
|
|
|
services.AddMemoryCache();
|
|
|
services.AddHttpContextAccessor();
|
|
|
services.AddMvc(options =>
|
|
|
{
|
|
|
options.Filters.Add(typeof(ApiAuthorizeFilter));
|
|
|
options.Filters.Add(typeof(ActionExecuteFilter));
|
|
|
// options.SuppressAsyncSuffixInActionNames = false;
|
|
|
});
|
|
|
services.AddControllers()
|
|
|
.AddNewtonsoftJson(op =>
|
|
|
{
|
|
|
op.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
|
|
|
op.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
|
|
});
|
|
|
|
|
|
Services.AddAuthentication(options =>
|
|
|
{
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
})
|
|
|
.AddJwtBearer(options =>
|
|
|
{
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
{
|
|
|
SaveSigninToken = true,//±£´ætoken,ºǫ́ÑéÖ¤tokenÊÇ·ñÉúЧ(ÖØÒª)
|
|
|
ValidateIssuer = true,//ÊÇ·ñÑéÖ¤Issuer
|
|
|
ValidateAudience = true,//ÊÇ·ñÑéÖ¤Audience
|
|
|
ValidateLifetime = true,//ÊÇ·ñÑé֤ʧЧʱ¼ä
|
|
|
ValidateIssuerSigningKey = true,//ÊÇ·ñÑéÖ¤SecurityKey
|
|
|
ValidAudience = AppSetting.Secret.Audience,//Audience
|
|
|
ValidIssuer = AppSetting.Secret.Issuer,//Issuer£¬ÕâÁ½ÏîºÍÇ°ÃæÇ©·¢jwtµÄÉèÖÃÒ»ÖÂ
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(AppSetting.Secret.JWT))
|
|
|
};
|
|
|
options.Events = new JwtBearerEvents()
|
|
|
{
|
|
|
OnChallenge = context =>
|
|
|
{
|
|
|
context.HandleResponse();
|
|
|
context.Response.Clear();
|
|
|
context.Response.ContentType = "application/json";
|
|
|
context.Response.StatusCode = 401;
|
|
|
context.Response.WriteAsync(new { message = "ÊÚȨδͨ¹ý", status = false, code = 401 }.Serialize());
|
|
|
return Task.CompletedTask;
|
|
|
}
|
|
|
};
|
|
|
});
|
|
|
//±ØÐëappsettings.jsonÖÐÅäÖÃ
|
|
|
string corsUrls = Configuration["CorsUrls"];
|
|
|
if (string.IsNullOrEmpty(corsUrls))
|
|
|
{
|
|
|
throw new Exception("ÇëÅäÖÿçÇëÇóµÄÇ°¶ËUrl");
|
|
|
}
|
|
|
services.AddCors(options =>
|
|
|
{
|
|
|
options.AddDefaultPolicy(
|
|
|
builder =>
|
|
|
{
|
|
|
builder.WithOrigins(corsUrls.Split(","))
|
|
|
//Ìí¼ÓÔ¤¼ìÇëÇó¹ýÆÚʱ¼ä
|
|
|
.SetPreflightMaxAge(TimeSpan.FromSeconds(2520))
|
|
|
.AllowCredentials()
|
|
|
.AllowAnyHeader().AllowAnyMethod();
|
|
|
});
|
|
|
});
|
|
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
|
services.AddControllers();
|
|
|
services.AddSwaggerGen(c =>
|
|
|
{
|
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "VOL.Coreºǫ́Api", Version = "v1" });
|
|
|
var security = new Dictionary<string, IEnumerable<string>>
|
|
|
{ { AppSetting.Secret.Issuer, new string[] { } }};
|
|
|
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
|
|
|
{
|
|
|
Description = "JWTÊÚȨtokenÇ°ÃæÐèÒª¼ÓÉÏ×Ö¶ÎBearerÓëÒ»¸ö¿Õ¸ñ,ÈçBearer token",
|
|
|
Name = "Authorization",
|
|
|
In = ParameterLocation.Header,
|
|
|
Type = SecuritySchemeType.ApiKey,
|
|
|
BearerFormat = "JWT",
|
|
|
Scheme = "Bearer"
|
|
|
});
|
|
|
|
|
|
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
|
{
|
|
|
{
|
|
|
new OpenApiSecurityScheme
|
|
|
{
|
|
|
Reference = new OpenApiReference {
|
|
|
Type = ReferenceType.SecurityScheme,
|
|
|
Id = "Bearer"
|
|
|
}
|
|
|
},
|
|
|
new string[] { }
|
|
|
}
|
|
|
});
|
|
|
})
|
|
|
.AddControllers()
|
|
|
.ConfigureApiBehaviorOptions(options =>
|
|
|
{
|
|
|
options.SuppressConsumesConstraintForFormFileParameters = true;
|
|
|
options.SuppressInferBindingSourcesForParameters = true;
|
|
|
options.SuppressModelStateInvalidFilter = true;
|
|
|
options.SuppressMapClientErrors = true;
|
|
|
options.ClientErrorMapping[404].Link =
|
|
|
"https://*/404";
|
|
|
});
|
|
|
//ApiBehaviorOptions
|
|
|
}
|
|
|
public void ConfigureContainer(ContainerBuilder builder)
|
|
|
{
|
|
|
Services.AddModule(builder, Configuration);
|
|
|
}
|
|
|
// 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.UseMiddleware<ExceptionHandlerMiddleWare>();
|
|
|
app.UseStaticFiles().UseStaticFiles(new StaticFileOptions
|
|
|
{
|
|
|
ServeUnknownFileTypes = true
|
|
|
});
|
|
|
app.UseDefaultFiles();
|
|
|
app.Use(HttpRequestMiddleware.Context);
|
|
|
app.UseStaticFiles(new StaticFileOptions()
|
|
|
{
|
|
|
FileProvider = new PhysicalFileProvider(
|
|
|
Path.Combine(Directory.GetCurrentDirectory(), @"Upload")),
|
|
|
//ÅäÖ÷ÃÎÊÐéÄâĿ¼ʱÎļþ¼Ð±ðÃû
|
|
|
RequestPath = "/Upload",
|
|
|
OnPrepareResponse = (Microsoft.AspNetCore.StaticFiles.StaticFileResponseContext staticFile) =>
|
|
|
{
|
|
|
//¿ÉÒÔÔÚ´Ë´¦¶ÁÈ¡ÇëÇóµÄÐÅÏ¢½øÐÐȨÏÞÈÏÖ¤
|
|
|
// staticFile.File
|
|
|
// staticFile.Context.Response.StatusCode;
|
|
|
}
|
|
|
});
|
|
|
//ÅäÖÃHttpContext
|
|
|
app.UseStaticHttpContext();
|
|
|
|
|
|
app.UseSwagger();
|
|
|
app.UseSwaggerUI(c =>
|
|
|
{
|
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "VOL.Coreºǫ́Api");
|
|
|
});
|
|
|
app.UseRouting();
|
|
|
//UseCors,UseAuthenticationgÁ½¸öλÖõÄ˳ÐòºÜÖØÒª
|
|
|
app.UseCors();
|
|
|
app.UseAuthentication();
|
|
|
app.UseAuthorization();
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
{
|
|
|
endpoints.MapControllerRoute(
|
|
|
name: "default",
|
|
|
pattern: "{controller=ApiHome}/{action=Index}/{id?}");
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|