From 9050818d9973379af565e8cc53cfa7b294f5b608 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sat, 15 Jun 2019 22:11:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=8D=95=E7=82=B9=E7=99=BB=E5=BD=95=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E5=88=B0UseBootstrapAdmin=E6=96=B9=E6=B3=95=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #Comment 依赖组件升级 Bootstrap.Security.Mvc 升级到2.2.7 Longbow.Web 升级到2.2.12 --- Bootstrap.Admin/Bootstrap.Admin.csproj | 2 +- Bootstrap.Admin/Startup.cs | 17 +++-------------- Bootstrap.Admin/appsettings.Development.json | 3 --- Bootstrap.Admin/appsettings.json | 11 ----------- .../Bootstrap.Client.DataAccess.csproj | 2 +- Bootstrap.Client/Bootstrap.Client.csproj | 2 +- Bootstrap.Client/Models/HeaderBarModel.cs | 15 +++++++++++---- Bootstrap.Client/Startup.cs | 19 +++---------------- Bootstrap.Client/appsettings.Development.json | 8 ++++---- Bootstrap.Client/appsettings.json | 9 +++------ .../Bootstrap.DataAccess.csproj | 2 +- 11 files changed, 28 insertions(+), 62 deletions(-) diff --git a/Bootstrap.Admin/Bootstrap.Admin.csproj b/Bootstrap.Admin/Bootstrap.Admin.csproj index d4a3305d..a64ed39a 100644 --- a/Bootstrap.Admin/Bootstrap.Admin.csproj +++ b/Bootstrap.Admin/Bootstrap.Admin.csproj @@ -14,7 +14,7 @@ - + diff --git a/Bootstrap.Admin/Startup.cs b/Bootstrap.Admin/Startup.cs index d9438495..cd6585e1 100644 --- a/Bootstrap.Admin/Startup.cs +++ b/Bootstrap.Admin/Startup.cs @@ -3,9 +3,7 @@ using Bootstrap.Security.Filter; 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.HttpOverrides; @@ -63,13 +61,10 @@ namespace Bootstrap.Admin services.AddDbAdapter(); services.AddIPLocator(DictHelper.ConfigIPLocator); services.AddOnlineUsers(); - 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(async (client, ex) => await SignalRManager.Send(client, ex)); services.AddResponseCompression(); + services.AddBootstrapAdminAuthentication(); services.AddMvc(options => { options.Filters.Add(); @@ -81,11 +76,6 @@ namespace Bootstrap.Admin options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; JsonConvert.DefaultSettings = () => options.SerializerSettings; }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); - services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => - { - options.Cookie.Path = "/"; - if (!string.IsNullOrEmpty(Configuration["Domain"])) options.Cookie.Domain = Configuration["Domain"]; - }); services.AddApiVersioning(option => { option.DefaultApiVersion = new ApiVersion(1, 0); @@ -132,8 +122,7 @@ namespace Bootstrap.Admin app.UseHttpsRedirection(); app.UseResponseCompression(); app.UseStaticFiles(); - app.UseAuthentication(); - app.UseBootstrapAdminAuthorization(RoleHelper.RetrieveRolesByUserName, RoleHelper.RetrieveRolesByUrl, AppHelper.RetrievesByUserName); + app.UseBootstrapAdminAuthentication(RoleHelper.RetrieveRolesByUserName, RoleHelper.RetrieveRolesByUrl, AppHelper.RetrievesByUserName); app.UseOnlineUsers(callback: TraceHelper.Save); app.UseCacheManagerCorsHandler(); app.UseSignalR(routes => { routes.MapHub("/NotiHub"); }); @@ -158,4 +147,4 @@ namespace Bootstrap.Admin }); } } -} \ No newline at end of file +} diff --git a/Bootstrap.Admin/appsettings.Development.json b/Bootstrap.Admin/appsettings.Development.json index 202ab083..5c9025b1 100644 --- a/Bootstrap.Admin/appsettings.Development.json +++ b/Bootstrap.Admin/appsettings.Development.json @@ -17,9 +17,6 @@ }, "SwaggerPathBase": "", "AllowOrigins": "http://localhost:49185", - "Domain": null, - "KeyPath": "..\\keys", - "DisableAutomaticKeyGeneration": false, "LongbowCache": { "Enabled": true, "CorsItems": [ diff --git a/Bootstrap.Admin/appsettings.json b/Bootstrap.Admin/appsettings.json index 25e5925c..98b3bca8 100644 --- a/Bootstrap.Admin/appsettings.json +++ b/Bootstrap.Admin/appsettings.json @@ -51,17 +51,6 @@ ], "SwaggerPathBase": "/BA", "AllowOrigins": "http://localhost,http://argo.zylweb.cn", - "Domain": null, - "KeyPath": "..\\keys", - "ApplicationName": "__bd__", - "ApplicationDiscriminator": "BootstrapAdmin", - "DisableAutomaticKeyGeneration": false, - "TokenValidateOption": { - "Issuer": "BA", - "Audience": "api", - "Expires": 5, - "SecurityKey": "BootstrapAdmin-V1.1" - }, "Sentry" : { "Dsn": "https://70bdfff562e84fa7b9a43d65924ab9ad@sentry.io/1469396" }, diff --git a/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj b/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj index 9d2af4e5..ef59ba48 100644 --- a/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj +++ b/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj @@ -14,7 +14,7 @@ - + diff --git a/Bootstrap.Client/Bootstrap.Client.csproj b/Bootstrap.Client/Bootstrap.Client.csproj index 126b4dbc..3f48285d 100644 --- a/Bootstrap.Client/Bootstrap.Client.csproj +++ b/Bootstrap.Client/Bootstrap.Client.csproj @@ -9,7 +9,7 @@ - + diff --git a/Bootstrap.Client/Models/HeaderBarModel.cs b/Bootstrap.Client/Models/HeaderBarModel.cs index cfe2636d..56c9bdfa 100644 --- a/Bootstrap.Client/Models/HeaderBarModel.cs +++ b/Bootstrap.Client/Models/HeaderBarModel.cs @@ -1,5 +1,6 @@ using Bootstrap.Client.DataAccess; using Longbow.Configuration; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Authentication.Cookies; using System; using System.Security.Principal; @@ -18,15 +19,21 @@ namespace Bootstrap.Client.Models public HeaderBarModel(IIdentity identity) { var user = UserHelper.RetrieveUserByUserName(identity.Name); - Icon = $"{ConfigurationManager.AppSettings["AuthHost"]}/{DictHelper.RetrieveIconFolderPath().Trim('~', '/')}/{user.Icon}"; DisplayName = user.DisplayName; UserName = user.UserName; SettingsUrl = DictHelper.RetrieveSettingsUrl(); ProfilesUrl = DictHelper.RetrieveProfilesUrl(); NotisUrl = DictHelper.RetrieveNotisUrl(); - var uriBuilder = new UriBuilder(ConfigurationManager.AppSettings["AuthHost"]); - uriBuilder.Path = uriBuilder.Path == "/" ? CookieAuthenticationDefaults.LogoutPath.Value : uriBuilder.Path + CookieAuthenticationDefaults.LogoutPath; + + // set LogoutUrl + var authHost = ConfigurationManager.Get().AuthHost; + var uriBuilder = new UriBuilder(authHost); + uriBuilder.Path = uriBuilder.Path == "/" ? CookieAuthenticationDefaults.LogoutPath.Value : $"{uriBuilder.Path.TrimEnd('/')}{CookieAuthenticationDefaults.LogoutPath.Value}"; LogoutUrl = uriBuilder.ToString(); + + // set Icon + var icon = $"/{DictHelper.RetrieveIconFolderPath().Trim('~', '/')}/{user.Icon}"; + Icon = $"{authHost.TrimEnd('/')}{icon}"; if (!string.IsNullOrEmpty(user.Css)) Theme = user.Css; } @@ -65,4 +72,4 @@ namespace Bootstrap.Client.Models /// public string NotisUrl { get; } } -} \ No newline at end of file +} diff --git a/Bootstrap.Client/Startup.cs b/Bootstrap.Client/Startup.cs index 97c435ac..b574587a 100644 --- a/Bootstrap.Client/Startup.cs +++ b/Bootstrap.Client/Startup.cs @@ -1,11 +1,9 @@ -using Bootstrap.Client.DataAccess; +using Bootstrap.Client.DataAccess; using Bootstrap.Security.DataAccess; using Bootstrap.Security.Filter; using Longbow.Web; using Longbow.Web.SignalR; -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; @@ -14,7 +12,6 @@ using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; -using System.IO; namespace Bootstrap.Client { @@ -41,12 +38,9 @@ namespace Bootstrap.Client services.AddConfigurationManager(Configuration); services.AddCacheManager(Configuration); services.AddDbAdapter(); - 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.AddResponseCompression(); + services.AddBootstrapAdminAuthentication(); services.AddMvc(options => { options.Filters.Add(); @@ -57,12 +51,6 @@ namespace Bootstrap.Client options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; JsonConvert.DefaultSettings = () => options.SerializerSettings; }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); - services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => - { - options.Cookie.Path = "/"; - if (!string.IsNullOrEmpty(Configuration["Domain"])) options.Cookie.Domain = Configuration["Domain"]; - options.RebuildRedirectUri(Configuration["AuthHost"]); - }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -84,8 +72,7 @@ namespace Bootstrap.Client app.UseResponseCompression(); app.UseStaticFiles(); app.UseCookiePolicy(); - app.UseAuthentication(); - app.UseBootstrapAdminAuthorization(RoleHelper.RetrieveRolesByUserName, RoleHelper.RetrieveRolesByUrl, DbHelper.RetrieveAppsByUserName); + app.UseBootstrapAdminAuthentication(RoleHelper.RetrieveRolesByUserName, RoleHelper.RetrieveRolesByUrl, DbHelper.RetrieveAppsByUserName); app.UseCacheManagerCorsHandler(); app.UseSignalR(routes => { routes.MapHub("/NotiHub"); }); app.UseMvc(routes => diff --git a/Bootstrap.Client/appsettings.Development.json b/Bootstrap.Client/appsettings.Development.json index a55f84de..2ddbfab9 100644 --- a/Bootstrap.Client/appsettings.Development.json +++ b/Bootstrap.Client/appsettings.Development.json @@ -30,8 +30,8 @@ } } ], - "KeyPath": "..\\keys", - "AuthHost": "http://localhost:50852", - "AllowOrigins": "http://localhost:50852", - "Domain": null + "BootstrapAdminOptions": { + "AuthHost": "http://localhost:50852" + }, + "AllowOrigins": "http://localhost:50852" } diff --git a/Bootstrap.Client/appsettings.json b/Bootstrap.Client/appsettings.json index 6c18a691..6a7e85d6 100644 --- a/Bootstrap.Client/appsettings.json +++ b/Bootstrap.Client/appsettings.json @@ -29,13 +29,10 @@ } ], "AppId": 2, - "AuthHost": "http://argo.zylweb.cn/BA", - "Domain": null, + "BootstrapAdminOptions": { + "AuthHost": "http://argo.zylweb.cn/BA" + }, "AllowOrigins": "http://localhost,http://argo.zylweb.cn", - "KeyPath": "..\\keys", - "ApplicationName": "__bd__", - "ApplicationDiscriminator": "BootstrapAdmin", - "DisableAutomaticKeyGeneration": true, "LongbowCache": { "Enabled": true, "CacheItems": [ diff --git a/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj b/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj index c5ef756f..8f09a72b 100644 --- a/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj +++ b/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj @@ -15,7 +15,7 @@ - +