2018-10-24 20:12:11 +08:00
|
|
|
|
using Bootstrap.DataAccess;
|
|
|
|
|
using Bootstrap.Security.Filter;
|
|
|
|
|
using Longbow.Logging;
|
|
|
|
|
using Longbow.Web;
|
|
|
|
|
using Longbow.Web.SignalR;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2018-10-24 20:31:44 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.Versioning;
|
2018-10-24 20:12:11 +08:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
2018-10-24 17:12:16 +08:00
|
|
|
|
using Swashbuckle.AspNetCore.Swagger;
|
2018-10-24 20:12:11 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace Bootstrap.Admin
|
|
|
|
|
{
|
2018-10-24 17:12:16 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
2018-10-24 20:12:11 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
2018-10-24 17:12:16 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-10-24 20:12:11 +08:00
|
|
|
|
/// <param name="configuration"></param>
|
|
|
|
|
public Startup(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
}
|
2018-11-01 18:44:49 +08:00
|
|
|
|
|
2018-10-24 17:12:16 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
2018-10-24 20:12:11 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public IConfiguration Configuration { get; }
|
2018-11-01 18:44:49 +08:00
|
|
|
|
|
2018-10-24 20:12:11 +08:00
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
2018-10-24 17:12:16 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-10-24 20:12:11 +08:00
|
|
|
|
/// <param name="services"></param>
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.Configure<CookiePolicyOptions>(options =>
|
|
|
|
|
{
|
|
|
|
|
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
|
|
|
|
|
options.CheckConsentNeeded = context => true;
|
|
|
|
|
options.MinimumSameSitePolicy = SameSiteMode.None;
|
|
|
|
|
});
|
|
|
|
|
services.AddCors();
|
|
|
|
|
services.AddLogging(builder => builder.AddFileLogger().AddDBLogger(ExceptionsHelper.Log));
|
|
|
|
|
services.AddConfigurationManager(Configuration);
|
|
|
|
|
services.AddCacheManager(Configuration);
|
2018-11-01 18:44:49 +08:00
|
|
|
|
services.AddDbAdapter(Configuration);
|
2018-10-24 20:12:11 +08:00
|
|
|
|
var dataProtectionBuilder = services.AddDataProtection(op => op.ApplicationDiscriminator = Configuration["ApplicationDiscriminator"])
|
|
|
|
|
.SetApplicationName(Configuration["ApplicationName"])
|
|
|
|
|
.PersistKeysToFileSystem(new DirectoryInfo(Configuration["KeyPath"]));
|
|
|
|
|
if (Configuration["DisableAutomaticKeyGeneration"] == "True") dataProtectionBuilder.DisableAutomaticKeyGeneration();
|
|
|
|
|
services.AddSignalR().AddJsonProtocalDefault();
|
|
|
|
|
services.AddSignalRExceptionFilterHandler<SignalRHub>(async (client, ex) => await SignalRManager.Send(client, ex));
|
2018-10-30 14:08:17 +08:00
|
|
|
|
services.AddResponseCompression();
|
2018-10-24 20:12:11 +08:00
|
|
|
|
services.AddMvc(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Filters.Add<BootstrapAdminAuthorizeFilter>();
|
|
|
|
|
options.Filters.Add<ExceptionFilter>();
|
|
|
|
|
options.Filters.Add<SignalRExceptionFilter<SignalRHub>>();
|
|
|
|
|
}).AddJsonOptions(options =>
|
|
|
|
|
{
|
|
|
|
|
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
|
|
|
|
|
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
|
JsonConvert.DefaultSettings = () => options.SerializerSettings;
|
|
|
|
|
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
|
|
|
|
|
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => options.Cookie.Path = "/");
|
2018-10-24 20:31:44 +08:00
|
|
|
|
services.AddApiVersioning(option =>
|
|
|
|
|
{
|
|
|
|
|
option.DefaultApiVersion = new ApiVersion(1, 0);
|
|
|
|
|
option.ReportApiVersions = true;
|
|
|
|
|
option.AssumeDefaultVersionWhenUnspecified = true;
|
|
|
|
|
option.ApiVersionReader = ApiVersionReader.Combine(new HeaderApiVersionReader("api-version"), new QueryStringApiVersionReader("api-version"));
|
|
|
|
|
});
|
2018-10-24 17:12:16 +08:00
|
|
|
|
services.AddSwaggerGen(options =>
|
|
|
|
|
{
|
|
|
|
|
options.SwaggerDoc("v1", new Info
|
|
|
|
|
{
|
|
|
|
|
Version = "v1",
|
|
|
|
|
Title = "BootstrapAdmin API"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//Set the comments path for the swagger json and ui.
|
|
|
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, "Bootstrap.Admin.xml");
|
|
|
|
|
options.IncludeXmlComments(xmlPath);
|
|
|
|
|
options.OperationFilter<HttpHeaderOperation>(); // 添加httpHeader参数
|
2018-10-24 20:12:11 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2018-11-01 18:44:49 +08:00
|
|
|
|
|
2018-10-24 20:12:11 +08:00
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
2018-10-24 17:12:16 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="app"></param>
|
2018-10-24 20:12:11 +08:00
|
|
|
|
/// <param name="env"></param>
|
|
|
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
app.UseExceptionHandler("/Home/Error");
|
|
|
|
|
app.UseHsts();
|
|
|
|
|
}
|
|
|
|
|
app.UseStatusCodePagesWithReExecute("/Home/Error/{0}");
|
|
|
|
|
app.UseCors(builder => builder.WithOrigins(Configuration["AllowOrigins"].Split(',', StringSplitOptions.RemoveEmptyEntries)).AllowAnyHeader().AllowAnyMethod().AllowCredentials());
|
|
|
|
|
app.UseHttpsRedirection();
|
2018-10-30 14:08:17 +08:00
|
|
|
|
app.UseResponseCompression();
|
2018-10-24 20:12:11 +08:00
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseBootstrapAdminAuthorization(userName => RoleHelper.RetrieveRolesByUserName(userName), url => RoleHelper.RetrieveRolesByUrl(url));
|
|
|
|
|
app.UseCacheManagerCorsHandler();
|
|
|
|
|
app.UseSignalR(routes => { routes.MapHub<SignalRHub>("/NotiHub"); });
|
|
|
|
|
app.UseMvc(routes =>
|
|
|
|
|
{
|
|
|
|
|
routes.MapRoute(
|
|
|
|
|
name: "default",
|
|
|
|
|
template: "{controller=Home}/{action=Index}/{id?}");
|
|
|
|
|
});
|
2018-10-25 13:29:12 +08:00
|
|
|
|
app.UseWhen(context => context.Request.Path.StartsWithSegments("/swagger"), builder =>
|
2018-10-24 20:12:11 +08:00
|
|
|
|
{
|
|
|
|
|
builder.Use(async (context, next) =>
|
|
|
|
|
{
|
|
|
|
|
if (!context.User.Identity.IsAuthenticated) await context.ChallengeAsync();
|
|
|
|
|
else await next();
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-10-24 17:12:16 +08:00
|
|
|
|
app.UseSwagger();
|
|
|
|
|
app.UseSwaggerUI(c =>
|
|
|
|
|
{
|
2018-10-26 09:53:00 +08:00
|
|
|
|
c.SwaggerEndpoint($"{Configuration["SwaggerPathBase"]}/swagger/v1/swagger.json", "BootstrapAdmin API V1");
|
2018-10-24 20:12:11 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-11 19:58:12 +08:00
|
|
|
|
}
|