BootstrapAdmin/Bootstrap.Admin/Startup.cs

79 lines
3.0 KiB
C#
Raw Normal View History

2018-06-07 00:45:47 +08:00
using Bootstrap.Security.Filter;
using Bootstrap.Security.Middleware;
using Longbow.Cache;
using Longbow.Cache.Middleware;
using Longbow.Configuration;
using Longbow.Data;
using Longbow.Logging;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Serialization;
using System;
using System.IO;
namespace Bootstrap.Admin
{
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.AddCors();
2018-06-07 00:45:47 +08:00
services.AddLogging(builder => builder.AddFileLogger());
services.AddConfigurationManager();
services.AddCacheManager();
services.AddDBAccessFactory();
services.AddDataProtection(op => op.ApplicationDiscriminator = "BootstrapAdmin")
.SetApplicationName("__bd__")
.DisableAutomaticKeyGeneration()
.PersistKeysToFileSystem(new DirectoryInfo(ConfigurationManager.AppSettings["KeyPath"]));
services.AddMvc(options =>
{
options.Filters.Add<BootstrapAdminAuthorizeFilter>();
options.Filters.Add<BootstrapAdminExceptionFilter>();
}).AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseCors(builder => builder.WithOrigins(Configuration["AllowOrigins"].Split(',', StringSplitOptions.RemoveEmptyEntries)).AllowAnyHeader().AllowAnyMethod().AllowCredentials());
2018-06-07 00:45:47 +08:00
app.UseStaticFiles();
app.UseAuthentication();
app.UseBootstrapRoleAuthorization();
app.UseCacheManagerCorsHandler();
2018-06-07 00:45:47 +08:00
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}