refactor: 重构代码单点登录移动到UseBootstrapAdmin方法中

#Comment
依赖组件升级
Bootstrap.Security.Mvc 升级到2.2.7
Longbow.Web 升级到2.2.12
This commit is contained in:
Argo Zhang 2019-06-15 22:11:40 +08:00
parent 5a16cd6c36
commit 9050818d99
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
11 changed files with 28 additions and 62 deletions

View File

@ -14,7 +14,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.6" />
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.7" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="3.1.2" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.7.9" />

View File

@ -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<SignalRHub>(async (client, ex) => await SignalRManager.Send(client, ex));
services.AddResponseCompression();
services.AddBootstrapAdminAuthentication();
services.AddMvc(options =>
{
options.Filters.Add<BootstrapAdminAuthorizeFilter>();
@ -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<SignalRHub>("/NotiHub"); });
@ -158,4 +147,4 @@ namespace Bootstrap.Admin
});
}
}
}
}

View File

@ -17,9 +17,6 @@
},
"SwaggerPathBase": "",
"AllowOrigins": "http://localhost:49185",
"Domain": null,
"KeyPath": "..\\keys",
"DisableAutomaticKeyGeneration": false,
"LongbowCache": {
"Enabled": true,
"CorsItems": [

View File

@ -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"
},

View File

@ -14,7 +14,7 @@
<PackageReference Include="Bootstrap.Security.DataAccess" Version="2.2.4" />
<PackageReference Include="Longbow.Cache" Version="2.2.10" />
<PackageReference Include="Longbow.Data" Version="2.3.2" />
<PackageReference Include="Longbow.Web" Version="2.2.11" />
<PackageReference Include="Longbow.Web" Version="2.2.12" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="2.2.4" />
</ItemGroup>

View File

@ -9,7 +9,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.6" />
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.7" />
<PackageReference Include="Longbow.Logging" Version="2.2.7" />
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

View File

@ -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<BootstrapAdminOptions>().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
/// </summary>
public string NotisUrl { get; }
}
}
}

View File

@ -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<BootstrapAdminAuthorizeFilter>();
@ -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<SignalRHub>("/NotiHub"); });
app.UseMvc(routes =>

View File

@ -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"
}

View File

@ -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": [

View File

@ -15,7 +15,7 @@
<PackageReference Include="Longbow.Data" Version="2.3.2" />
<PackageReference Include="Longbow.Logging" Version="2.2.7" />
<PackageReference Include="Longbow.Security.Cryptography" Version="1.3.0" />
<PackageReference Include="Longbow.Web" Version="2.2.11" />
<PackageReference Include="Longbow.Web" Version="2.2.12" />
<PackageReference Include="Longbow.Cache" Version="2.2.10" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="2.2.4" />
<PackageReference Include="PetaPoco.Extensions" Version="1.0.5" />