feat: 客户端登录后直接返回本应用

This commit is contained in:
Argo-Tianyi 2022-01-10 01:54:10 +08:00
parent c00895a848
commit 11984e20e7
14 changed files with 92 additions and 24 deletions

View File

@ -153,4 +153,20 @@ class DictService : IDict
}
return url;
}
/// <summary>
/// 通过指定 appId 获得配置首页地址
/// </summary>
/// <param name="appId"></param>
/// <returns></returns>
public string? GetHomeUrlByAppId(string? appId)
{
string? url = null;
if (!string.IsNullOrEmpty(appId))
{
var dicts = GetAll();
url = dicts.FirstOrDefault(d => d.Category == "应用首页" && d.Name.Equals(appId, StringComparison.OrdinalIgnoreCase) && d.Define == EnumDictDefine.System)?.Code;
}
return url;
}
}

View File

@ -180,4 +180,20 @@ class DictService : BaseDatabase, IDict
var dicts = GetAll();
return dicts.FirstOrDefault(d => d.Name == "头像路径" && d.Category == "头像地址" && d.Define == EnumDictDefine.System)?.Code ?? "images/uploder/";
}
/// <summary>
/// 通过指定 appId 获得配置首页地址
/// </summary>
/// <param name="appId"></param>
/// <returns></returns>
public string? GetHomeUrlByAppId(string? appId)
{
string? url = null;
if (!string.IsNullOrEmpty(appId))
{
var dicts = GetAll();
url = dicts.FirstOrDefault(d => d.Category == "应用首页" && d.Name.Equals(appId, StringComparison.OrdinalIgnoreCase) && d.Define == EnumDictDefine.System)?.Code;
}
return url;
}
}

View File

@ -142,4 +142,11 @@ public interface IDict
/// </summary>
/// <returns></returns>
string RetrieveIconFolderPath();
/// <summary>
/// 通过指定 appId 获得配置首页地址
/// </summary>
/// <param name="appId"></param>
/// <returns></returns>
string? GetHomeUrlByAppId(string? appId);
}

View File

@ -28,6 +28,12 @@ public partial class AdminLogin
[Parameter]
public string? ReturnUrl { get; set; }
/// <summary>
///
/// </summary>
[Parameter]
public string? AppId { get; set; }
[Inject]
[NotNull]
private IDict? DictsService { get; set; }
@ -49,7 +55,11 @@ public partial class AdminLogin
Title = DictsService.GetWebTitle();
PostUrl = QueryHelper.AddQueryString("/Account/Login", "ReturnUrl", ReturnUrl ?? "");
PostUrl = QueryHelper.AddQueryString("/Account/Login", new Dictionary<string, string?>
{
["ReturnUrl"] = ReturnUrl,
["AppId"] = AppId
});
}
void OnClickSwitchButton()
@ -58,6 +68,7 @@ public partial class AdminLogin
PostUrl = QueryHelper.AddQueryString(UseMobileLogin ? "/Account/Mobile" : "/Account/Login", new Dictionary<string, string?>()
{
[nameof(ReturnUrl)] = ReturnUrl,
["AppId"] = AppId,
["remember"] = rem
});
}

View File

@ -27,11 +27,12 @@ namespace BootstrapAdmin.Web.Controllers
/// <param name="password">Password.</param>
/// <param name="remember">Remember.</param>
/// <param name="returnUrl"></param>
/// <param name="appId"></param>
/// <param name="userService"></param>
/// <param name="dictService"></param>
/// <param name="loginService"></param>
[HttpPost]
public async Task<IActionResult> Login(string userName, string password, [FromQuery] string? remember, [FromQuery] string? returnUrl,
public async Task<IActionResult> Login(string userName, string password, [FromQuery] string? remember, [FromQuery] string? returnUrl, [FromQuery] string? appId,
[FromServices] IUser userService,
[FromServices] IDict dictService,
[FromServices] ILogin loginService)
@ -51,10 +52,10 @@ namespace BootstrapAdmin.Web.Controllers
}
await loginService.Log(userName, auth);
return auth ? await SignInAsync(userName, persistent, period, returnUrl) : RedirectLogin(returnUrl);
return auth ? await SignInAsync(userName, returnUrl ?? GetAppHomeUrl(dictService, appId), persistent, period) : RedirectLogin(returnUrl);
}
private async Task<IActionResult> SignInAsync(string userName, bool persistent, int period = 0, string? returnUrl = null, string authenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme)
private async Task<IActionResult> SignInAsync(string userName, string returnUrl, bool persistent, int period = 0, string authenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme)
{
var identity = new ClaimsIdentity(authenticationScheme);
identity.AddClaim(new Claim(ClaimTypes.Name, userName));
@ -70,9 +71,17 @@ namespace BootstrapAdmin.Web.Controllers
}
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity), properties);
return Redirect(returnUrl ?? "/Home/Index");
return Redirect(returnUrl);
}
/// <summary>
/// 通过 appId 获取设置的首页
/// </summary>
/// <param name="dictService"></param>
/// <param name="appId"></param>
/// <returns></returns>
private static string GetAppHomeUrl(IDict dictService, string? appId) => dictService.GetHomeUrlByAppId(appId) ?? "/Admin/Index";
private IActionResult RedirectLogin(string? returnUrl = null)
{
var query = Request.Query.Aggregate(new Dictionary<string, string?>(), (d, v) =>
@ -108,13 +117,17 @@ namespace BootstrapAdmin.Web.Controllers
/// <returns></returns>
[HttpPost()]
public async Task<IActionResult> Mobile(string phone, string code, [FromQuery] string? remember, [FromQuery] string? returnUrl,
[FromQuery] string? appId,
[FromServices] ISMSProvider provider,
[FromServices] ILogin loginService,
[FromServices] IUser userService,
[FromServices] IDict dictService,
[FromServices] BootstrapAppContext context)
{
if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(code)) return RedirectLogin();
if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(code))
{
return RedirectLogin();
}
var auth = provider.Validate(phone, code);
var persistent = remember == "true";
@ -129,7 +142,7 @@ namespace BootstrapAdmin.Web.Controllers
{
userService.TryCreateUserByPhone(phone, code, context.AppId, provider.Options.Roles);
}
return auth ? await SignInAsync(phone, persistent, period, returnUrl, MobileSchema) : RedirectLogin(returnUrl);
return auth ? await SignInAsync(phone, returnUrl ?? GetAppHomeUrl(dictService, appId), persistent, period, MobileSchema) : RedirectLogin(returnUrl);
}
#endregion

View File

@ -3,6 +3,6 @@
<div class="wrap">
<div class="container">
<AdminLogin ReturnUrl="@ReturnUrl" />
<AdminLogin ReturnUrl="@ReturnUrl" AppId="@AppId" />
</div>
</div>

View File

@ -11,4 +11,11 @@ public partial class Login
[SupplyParameterFromQuery]
[Parameter]
public string? ReturnUrl { get; set; }
/// <summary>
///
/// </summary>
[SupplyParameterFromQuery]
[Parameter]
public string? AppId { get; set; }
}

View File

@ -5,8 +5,6 @@ namespace BootstrapAdmin.Web.Pages.Home;
/// <summary>
///
/// </summary>
[Route("/")]
[Route("/Home")]
[Route("/Home/Index")]
[Authorize]
public class Index : ComponentBase

View File

@ -85,6 +85,8 @@ namespace BootstrapClient.Web.Shared.Shared
[NotNull]
private IConfiguration? Configuration { get; set; }
private string? AppId { get; set; }
/// <summary>
/// OnInitialized 方法
/// </summary>
@ -92,11 +94,10 @@ namespace BootstrapClient.Web.Shared.Shared
{
base.OnInitialized();
// TODO: 后期重构 AppId 到统一的地方
var appId = Configuration.GetValue("AppId", "Blazor");
ProfileUrl = CombinePath(DictsService.GetProfileUrl(appId));
SettingsUrl = CombinePath(DictsService.GetSettingsUrl(appId));
NotificationUrl = CombinePath(DictsService.GetNotificationUrl(appId));
AppId = Configuration.GetValue("AppId", "Blazor");
ProfileUrl = CombinePath(DictsService.GetProfileUrl(AppId));
SettingsUrl = CombinePath(DictsService.GetSettingsUrl(AppId));
NotificationUrl = CombinePath(DictsService.GetNotificationUrl(AppId));
}
private string CombinePath(string? url)
@ -129,8 +130,8 @@ namespace BootstrapClient.Web.Shared.Shared
private Task<bool> OnAuthorizing(string url) => SecurityService.AuhorizingNavigation(UserName, url);
private string LogoutUrl => CombinePath($"/Account/Logout");
private string LogoutUrl => CombinePath($"/Account/Logout?AppId={AppId}");
private string AuthorUrl => CombinePath($"/Account/Login?ReturnUrl={NavigationManager.Uri}");
private string AuthorUrl => CombinePath($"/Account/Login?ReturnUrl={NavigationManager.Uri}&AppId={AppId}");
}
}

View File

@ -29,7 +29,7 @@ namespace Microsoft.Extensions.DependencyInjection
services.AddPetaPocoDataAccessServices((provider, builder) =>
{
var configuration = provider.GetRequiredService<IConfiguration>();
var connString = configuration.GetConnectionString("bb");
var connString = configuration.GetConnectionString("ba");
builder.UsingProvider<SQLiteDatabaseProvider>()
.UsingConnectionString(connString);
});

View File

@ -3,7 +3,7 @@
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:13998",
"applicationUrl": "http://localhost:49185",
"sslPort": 0
}
},
@ -19,7 +19,7 @@
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"applicationUrl": "http://localhost:13998",
"applicationUrl": "http://localhost:49185",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}

View File

@ -7,7 +7,7 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AppId": "Blazor",
"AppId": "Demo",
"SimulateUserName": "",
"BootstrapAdminAuthenticationOptions": {
"AuthHost": "http://localhost:5210",

View File

@ -8,13 +8,12 @@
},
"AllowedHosts": "*",
"AllowOrigins": "",
"AppId": "Blazor",
"AppId": "Demo",
"BootstrapAdminAuthenticationOptions": {
"AuthHost": "http://admin.blazor.zone/"
},
"ConnectionStrings": {
"ba": "Data Source=.;Initial Catalog=BootstrapAdmin;User ID=sa;Password=sa",
"bb": "Data Source=..\\..\\admin\\BootstrapAdmin.Web\\BootstrapAdmin.db;",
"ba": "Data Source=..\\..\\admin\\BootstrapAdmin.Web\\BootstrapAdmin.db;",
"client": "Data Source=Client.db;"
},
"DB": [