diff --git a/BootstrapAdmin.sln b/BootstrapAdmin.sln index 11a99198..eb6ced01 100644 --- a/BootstrapAdmin.sln +++ b/BootstrapAdmin.sln @@ -84,6 +84,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docker", "docker", "{85574E src\admin\Bootstrap.Admin\Linux.Dockerfile = src\admin\Bootstrap.Admin\Linux.Dockerfile EndProjectSection EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bootstrap.Security.Mvc", "..\Utility\Bootstrap.Security.Mvc\src\Bootstrap.Security.Mvc\Bootstrap.Security.Mvc.csproj", "{A44BB5C0-BB27-482B-B1F1-DF79AC070C4E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -118,6 +120,10 @@ Global {BC1C6D63-ADA9-4C3B-89F0-CEB191A86BF5}.Debug|Any CPU.Build.0 = Debug|Any CPU {BC1C6D63-ADA9-4C3B-89F0-CEB191A86BF5}.Release|Any CPU.ActiveCfg = Release|Any CPU {BC1C6D63-ADA9-4C3B-89F0-CEB191A86BF5}.Release|Any CPU.Build.0 = Release|Any CPU + {A44BB5C0-BB27-482B-B1F1-DF79AC070C4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A44BB5C0-BB27-482B-B1F1-DF79AC070C4E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A44BB5C0-BB27-482B-B1F1-DF79AC070C4E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A44BB5C0-BB27-482B-B1F1-DF79AC070C4E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -137,6 +143,7 @@ Global {843811A2-FE49-410F-BF9F-9F1FB14A1DEE} = {C7F51A14-2D89-4D1F-AD78-C42B79AB0BF0} {BC1C6D63-ADA9-4C3B-89F0-CEB191A86BF5} = {C7F51A14-2D89-4D1F-AD78-C42B79AB0BF0} {E03B7391-B52F-4449-B400-5CD9DE01F085} = {41B6D37A-5E5E-42B3-85E4-D81A81E3D757} + {A44BB5C0-BB27-482B-B1F1-DF79AC070C4E} = {E03B7391-B52F-4449-B400-5CD9DE01F085} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {221EAE38-5F75-4391-9A48-E462A9F3B8FC} diff --git a/Directory.Build.props b/Directory.Build.props index 9df49bac..855a1272 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -12,7 +12,7 @@ true - + diff --git a/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj b/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj index 82b380c2..39d313ec 100644 --- a/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj +++ b/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj @@ -1,9 +1,10 @@  - netcoreapp2.2 + netcoreapp3.0 + true - + $(MSBuildProjectName).xml Windows @@ -11,18 +12,14 @@ - - - - + - - + diff --git a/src/admin/Bootstrap.Admin/Controllers/AdminController.cs b/src/admin/Bootstrap.Admin/Controllers/AdminController.cs index 9d009c90..d426dcbf 100644 --- a/src/admin/Bootstrap.Admin/Controllers/AdminController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/AdminController.cs @@ -102,7 +102,7 @@ namespace Bootstrap.Admin.Controllers /// /// /// - public ActionResult Profiles([FromServices]IHostingEnvironment host) => View(new ProfilesModel(this, host)); + public ActionResult Profiles([FromServices]IWebHostEnvironment host) => View(new ProfilesModel(this, host)); /// /// diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/ProfilesController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/ProfilesController.cs index 3f3240c4..f4f83be9 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/ProfilesController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/ProfilesController.cs @@ -1,4 +1,4 @@ -using Bootstrap.DataAccess; +using Bootstrap.DataAccess; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -24,7 +24,7 @@ namespace Bootstrap.Admin.Controllers.Api /// [HttpPost("{id}")] [ButtonAuthorize(Url = "~/Admin/Profiles", Auth = "saveIcon")] - public JsonResult Post(string id, [FromServices]IHostingEnvironment env, [FromForm]DeleteFileCollection files) + public JsonResult Post(string id, [FromServices]IWebHostEnvironment env, [FromForm]DeleteFileCollection files) { if (!id.Equals("Delete", StringComparison.OrdinalIgnoreCase)) return new JsonResult(new object()); @@ -72,7 +72,7 @@ namespace Bootstrap.Admin.Controllers.Api /// [HttpPost] [ButtonAuthorize(Url = "~/Admin/Profiles", Auth = "saveIcon")] - public async Task Post([FromServices]IHostingEnvironment env, IFormCollection files) + public async Task Post([FromServices]IWebHostEnvironment env, IFormCollection files) { var previewUrl = string.Empty; long fileSize = 0; @@ -128,4 +128,4 @@ namespace Bootstrap.Admin.Controllers.Api return ret; } } -} \ No newline at end of file +} diff --git a/src/admin/Bootstrap.Admin/HttpHeaderOperation.cs b/src/admin/Bootstrap.Admin/HttpHeaderOperation.cs index a782e2c6..d2da5ce6 100644 --- a/src/admin/Bootstrap.Admin/HttpHeaderOperation.cs +++ b/src/admin/Bootstrap.Admin/HttpHeaderOperation.cs @@ -1,35 +1,33 @@ -using Microsoft.AspNetCore.Authorization; -using Swashbuckle.AspNetCore.Swagger; -using Swashbuckle.AspNetCore.SwaggerGen; -using System.Collections.Generic; -using Operation = Swashbuckle.AspNetCore.Swagger.Operation; +//using Microsoft.AspNetCore.Authorization; +//using Microsoft.OpenApi.Models; +//using Swashbuckle.AspNetCore.SwaggerGen; +//using System.Collections.Generic; -namespace Bootstrap.Admin -{ - /// - /// - /// - public class HttpHeaderOperation : IOperationFilter - { - /// - /// - /// - /// - /// - public void Apply(Operation operation, OperationFilterContext context) - { - if (operation.Parameters == null) operation.Parameters = new List(); +//namespace Bootstrap.Admin +//{ +// /// +// /// +// /// +// public class HttpHeaderOperation : IOperationFilter +// { +// /// +// /// +// /// +// /// +// /// +// public void Apply(OpenApiOperation operation, OperationFilterContext context) +// { +// if (operation.Parameters == null) operation.Parameters = new List(); - if (context.MethodInfo.GetCustomAttributes(typeof(AllowAnonymousAttribute), true).Length == 0) - { - operation.Parameters.Add(new NonBodyParameter() - { - Name = "Authorization", //添加Authorization头部参数 - In = "header", - Type = "string", - Required = false - }); - } - } - } -} +// if (context.MethodInfo.GetCustomAttributes(typeof(AllowAnonymousAttribute), true).Length == 0) +// { +// operation.Parameters.Add(new OpenApiParameter() +// { +// Name = "Authorization", //添加Authorization头部参数 +// In = ParameterLocation.Header, +// Required = false +// }); +// } +// } +// } +//} diff --git a/src/admin/Bootstrap.Admin/Models/ProfilesModel.cs b/src/admin/Bootstrap.Admin/Models/ProfilesModel.cs index 4470c3e5..49f71be5 100644 --- a/src/admin/Bootstrap.Admin/Models/ProfilesModel.cs +++ b/src/admin/Bootstrap.Admin/Models/ProfilesModel.cs @@ -30,7 +30,7 @@ namespace Bootstrap.Admin.Models /// /// /// - public ProfilesModel(ControllerBase controller, IHostingEnvironment host) : base(controller) + public ProfilesModel(ControllerBase controller, IWebHostEnvironment host) : base(controller) { if (host != null) { diff --git a/src/admin/Bootstrap.Admin/Program.cs b/src/admin/Bootstrap.Admin/Program.cs index 7aec9ac7..c84c161b 100644 --- a/src/admin/Bootstrap.Admin/Program.cs +++ b/src/admin/Bootstrap.Admin/Program.cs @@ -1,5 +1,5 @@ -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; namespace Bootstrap.Admin { @@ -14,14 +14,14 @@ namespace Bootstrap.Admin /// public static void Main(string[] args) { - CreateWebHostBuilder(args).Build().Run(); + CreateHostBuilder(args).Build().Run(); } /// /// /// /// /// - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args).UseSentry().UseStartup(); + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(builder => builder.UseStartup()); } } diff --git a/src/admin/Bootstrap.Admin/Startup.cs b/src/admin/Bootstrap.Admin/Startup.cs index ce3f2689..308aa928 100644 --- a/src/admin/Bootstrap.Admin/Startup.cs +++ b/src/admin/Bootstrap.Admin/Startup.cs @@ -1,16 +1,15 @@ using Bootstrap.DataAccess; using Longbow.Web; using Longbow.Web.SignalR; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Versioning; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; +using Microsoft.Extensions.Hosting; using System; using System.Text.Encodings.Web; using System.Text.Unicode; @@ -43,7 +42,7 @@ namespace Bootstrap.Admin /// public void ConfigureServices(IServiceCollection services) { - services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All)); + //services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All)); services.Configure(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. @@ -51,39 +50,35 @@ namespace Bootstrap.Admin options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddCors(); - services.AddLogging(builder => builder.AddFileLogger().AddDBLogger(ExceptionsHelper.Log)); + //services.AddLogging(builder => builder.AddFileLogger().AddDBLogger(ExceptionsHelper.Log)); services.AddConfigurationManager(); services.AddCacheManager(); services.AddDbAdapter(); services.AddIPLocator(DictHelper.ConfigIPLocator); services.AddOnlineUsers(); - services.AddSignalR().AddJsonProtocalDefault(); - services.AddSignalRExceptionFilterHandler((client, ex) => client.SendMessageBody(ex).ConfigureAwait(false)); + //services.AddSignalR().AddJsonProtocalDefault(); + //services.AddSignalRExceptionFilterHandler((client, ex) => client.SendMessageBody(ex).ConfigureAwait(false)); services.AddResponseCompression(); services.AddBootstrapAdminAuthentication().AddGitee(OAuthHelper.Configure).AddGitHub(OAuthHelper.Configure); - services.AddSwagger(); + services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()); + //services.AddSwagger(); services.AddButtonAuthorization(MenuHelper.AuthorizateButtons); services.AddBootstrapAdminBackgroundTask(); services.AddHttpClient(); services.AddAdminHealthChecks(); - services.AddMvc(options => + services.AddControllersWithViews(options => { options.Filters.Add(); options.Filters.Add(); options.Filters.Add>(); - }).AddJsonOptions(options => - { - options.SerializerSettings.ContractResolver = new DefaultContractResolver(); - options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; - JsonConvert.DefaultSettings = () => options.SerializerSettings; - }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); - 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")); }); + //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")); + //}); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -92,7 +87,7 @@ namespace Bootstrap.Admin /// /// /// - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseForwardedHeaders(new ForwardedHeadersOptions() { ForwardedHeaders = ForwardedHeaders.All }); if (env.IsDevelopment()) @@ -109,21 +104,20 @@ namespace Bootstrap.Admin app.UseHttpsRedirection(); app.UseResponseCompression(); app.UseStaticFiles(); + app.UseRouting(); + app.UseAuthentication(); + app.UseAuthorization(); app.UseBootstrapAdminAuthentication(RoleHelper.RetrievesByUserName, RoleHelper.RetrievesByUrl, AppHelper.RetrievesByUserName); app.UseBootstrapHealthChecks(); app.UseOnlineUsers(TraceHelper.Filter, TraceHelper.Save); app.UseCacheManager(); - app.UseSignalR(routes => + //app.UseSwagger(Configuration["SwaggerPathBase"].TrimEnd('/')); + app.UseEndpoints(endpoints => { - routes.MapHub("/NotiHub"); - routes.MapHub("/TaskLogHub"); - }); - app.UseSwagger(Configuration["SwaggerPathBase"].TrimEnd('/')); - app.UseMvc(routes => - { - routes.MapRoute( - name: "default", - template: "{controller=Home}/{action=Index}/{id?}"); + //endpoints.MapHub("/NotiHub"); + //endpoints.MapHub("/TaskLogHub"); + //endpoints.MapHealthChecks("/healths"); + endpoints.MapDefaultControllerRoute().RequireAuthorization(); }); } } diff --git a/src/admin/Bootstrap.Admin/SwaggerExtensions.cs b/src/admin/Bootstrap.Admin/SwaggerExtensions.cs index bd770dd0..d07e269e 100644 --- a/src/admin/Bootstrap.Admin/SwaggerExtensions.cs +++ b/src/admin/Bootstrap.Admin/SwaggerExtensions.cs @@ -1,58 +1,59 @@ -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.DependencyInjection; -using Swashbuckle.AspNetCore.Swagger; -using System; -using System.IO; +//using Microsoft.AspNetCore.Authentication; +//using Microsoft.AspNetCore.Builder; +//using Microsoft.Extensions.DependencyInjection; +//using Microsoft.OpenApi.Models; +//using Swashbuckle.AspNetCore.Swagger; +//using System; +//using System.IO; -namespace Bootstrap.Admin -{ - /// - /// - /// - internal static class SwaggerExtensions - { - /// - /// - /// - /// - /// - public static void UseSwagger(this IApplicationBuilder app, string pathBase) - { - app.UseWhen(context => context.Request.Path.StartsWithSegments("/swagger"), builder => - { - builder.Use(async (context, next) => - { - if (!context.User.Identity.IsAuthenticated) await context.ChallengeAsync(); - else await next(); - }); - }); - app.UseSwagger(); - app.UseSwaggerUI(c => - { - c.SwaggerEndpoint($"{pathBase}/swagger/v1/swagger.json", "BootstrapAdmin API V1"); - }); - } +//namespace Bootstrap.Admin +//{ +// /// +// /// +// /// +// internal static class SwaggerExtensions +// { +// /// +// /// +// /// +// /// +// /// +// public static void UseSwagger(this IApplicationBuilder app, string pathBase) +// { +// app.UseWhen(context => context.Request.Path.StartsWithSegments("/swagger"), builder => +// { +// builder.Use(async (context, next) => +// { +// if (!context.User.Identity.IsAuthenticated) await context.ChallengeAsync(); +// else await next(); +// }); +// }); +// app.UseSwagger(); +// app.UseSwaggerUI(c => +// { +// c.SwaggerEndpoint($"{pathBase}/swagger/v1/swagger.json", "BootstrapAdmin API V1"); +// }); +// } - /// - /// - /// - /// - public static void AddSwagger(this IServiceCollection services) - { - services.AddSwaggerGen(options => - { - options.SwaggerDoc("v1", new Info - { - Version = "v1", - Title = "BootstrapAdmin API" - }); +// /// +// /// +// /// +// /// +// public static void AddSwagger(this IServiceCollection services) +// { +// services.AddSwaggerGen(options => +// { +// options.SwaggerDoc("v1", new OpenApiInfo +// { +// 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(); // 添加httpHeader参数 - }); - } - } -} +// //Set the comments path for the swagger json and ui. +// var xmlPath = Path.Combine(AppContext.BaseDirectory, "Bootstrap.Admin.xml"); +// options.IncludeXmlComments(xmlPath); +// options.OperationFilter(); // 添加httpHeader参数 +// }); +// } +// } +//} diff --git a/test/UnitTest/Bootstrap.DataAccess/SQLServer/UsersTest.cs b/test/UnitTest/Bootstrap.DataAccess/SQLServer/UsersTest.cs index dd9a5566..de6094be 100644 --- a/test/UnitTest/Bootstrap.DataAccess/SQLServer/UsersTest.cs +++ b/test/UnitTest/Bootstrap.DataAccess/SQLServer/UsersTest.cs @@ -18,7 +18,7 @@ namespace Bootstrap.DataAccess.SqlServer [Fact] public void Authenticate_Ok() { - Assert.True(UserHelper.Authenticate("Admin", "123789", u => u.Ip = "::1")); + Assert.True(UserHelper.Authenticate("Admin", "123789")); } /// @@ -27,7 +27,7 @@ namespace Bootstrap.DataAccess.SqlServer [Fact] public void Authenticate_Fail() { - Assert.False(UserHelper.Authenticate("Admin-NotExists", "123789", u => u.Ip = "::1")); + Assert.False(UserHelper.Authenticate("Admin-NotExists", "123789")); } [Fact] diff --git a/test/UnitTest/UnitTest.csproj b/test/UnitTest/UnitTest.csproj index c9c08401..b06a8b20 100644 --- a/test/UnitTest/UnitTest.csproj +++ b/test/UnitTest/UnitTest.csproj @@ -1,7 +1,7 @@  - netcoreapp2.2 + netcoreapp3.0 false full @@ -11,7 +11,6 @@ all runtime; build; native; contentfiles; analyzers -