feat: 改用表单提交方式进行用户鉴权
This commit is contained in:
parent
0df111ee42
commit
1d94e53866
|
@ -31,6 +31,12 @@ namespace BootstrapAdmin.Web.Components
|
|||
[Parameter]
|
||||
public EventCallback<bool> ToggleStateChanged { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
public EventCallback<MouseEventArgs> OnClick { get; set; }
|
||||
|
||||
private async Task Toggle()
|
||||
{
|
||||
ToggleState = !ToggleState;
|
||||
|
@ -38,6 +44,10 @@ namespace BootstrapAdmin.Web.Components
|
|||
{
|
||||
await ToggleStateChanged.InvokeAsync(ToggleState);
|
||||
}
|
||||
if (OnClick.HasDelegate)
|
||||
{
|
||||
await OnClick.InvokeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private string? GetText() => ToggleState ? OnText : OffText;
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace Bootstrap.Admin.Controllers
|
|||
/// Account controller.
|
||||
/// </summary>
|
||||
[AllowAnonymous]
|
||||
public class LoginController : Controller
|
||||
public class AccountController : Controller
|
||||
{
|
||||
//private const string MobileSchema = "Mobile";
|
||||
///// <summary>
|
||||
|
@ -55,43 +55,35 @@ namespace Bootstrap.Admin.Controllers
|
|||
// return ret;
|
||||
//}
|
||||
|
||||
///// <summary>
|
||||
///// Login the specified userName, password and remember.
|
||||
///// </summary>
|
||||
///// <returns>The login.</returns>
|
||||
///// <param name="userService"></param>
|
||||
///// <param name="loginService"></param>
|
||||
///// <param name="context"></param>
|
||||
///// <param name="userName">User name.</param>
|
||||
///// <param name="password">Password.</param>
|
||||
///// <param name="remember">Remember.</param>
|
||||
//[HttpPost]
|
||||
//public async Task<IActionResult> Login(string userName, string password, string remember,
|
||||
// [FromServices] IUsers userService,
|
||||
// [FromServices] ILogins loginService,
|
||||
// [FromServices] BootstrapAppContext context)
|
||||
//{
|
||||
// if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
|
||||
// {
|
||||
// return RedirectLogin();
|
||||
// }
|
||||
|
||||
// var auth = userService.Authenticate(userName, password);
|
||||
// await loginService.Log(userName, auth);
|
||||
// if (auth)
|
||||
// {
|
||||
// context.UserName = userName;
|
||||
// }
|
||||
// return auth ? await SignInAsync(userName, remember == "true") : RedirectLogin();
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// Login the specified userName, password and remember.
|
||||
/// </summary>
|
||||
[HttpGet("{id}")]
|
||||
public async Task<IActionResult> Index([FromServices]IUsers user, [FromServices] LoginService loginService, [FromQuery] string? id) => loginService.Valid(id)
|
||||
? await SignInAsync(loginService.UserName, loginService.Remember)
|
||||
: Redirect(CookieAuthenticationDefaults.LoginPath);
|
||||
/// <returns>The login.</returns>
|
||||
/// <param name="userService"></param>
|
||||
/// <param name="loginService"></param>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="userName">User name.</param>
|
||||
/// <param name="password">Password.</param>
|
||||
/// <param name="remember">Remember.</param>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Login(string userName, string password, string remember,
|
||||
[FromServices] IUsers userService,
|
||||
[FromServices] ILogins loginService,
|
||||
[FromServices] BootstrapAppContext context)
|
||||
{
|
||||
if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
|
||||
{
|
||||
return RedirectLogin();
|
||||
}
|
||||
|
||||
var auth = userService.Authenticate(userName, password);
|
||||
await loginService.Log(userName, auth);
|
||||
if (auth)
|
||||
{
|
||||
context.UserName = userName;
|
||||
}
|
||||
return auth ? await SignInAsync(userName, remember == "true") : RedirectLogin();
|
||||
}
|
||||
|
||||
private async Task<IActionResult> SignInAsync(string userName, bool persistent, string authenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme)
|
||||
{
|
|
@ -3,20 +3,11 @@
|
|||
|
||||
<div class="wrap">
|
||||
<div class="container">
|
||||
<form method="post" class="form-signin">
|
||||
<form method="post" class="form-signin" action="@PostUrl">
|
||||
<h2 class="form-signin-heading">@Title</h2>
|
||||
<div class="login-wrap">
|
||||
<Block Condition="!AllowMobile">
|
||||
<BootstrapInputGroup>
|
||||
<BootstrapInputGroupIcon Icon="fa fa-fw fa-user" />
|
||||
<BootstrapInput TValue="string" @bind-Value="UserName" maxlength="16" IsAutoFocus="true" PlaceHolder="用户名" />
|
||||
</BootstrapInputGroup>
|
||||
<BootstrapInputGroup class="mt-3">
|
||||
<BootstrapInputGroupIcon Icon="fa fa-fw fa-lock" />
|
||||
<BootstrapPassword @bind-Value="Password" PlaceHolder="密码" />
|
||||
</BootstrapInputGroup>
|
||||
</Block>
|
||||
<Block Condition="AllowMobile">
|
||||
@if (UseMobileLogin)
|
||||
{
|
||||
<BootstrapInputGroup>
|
||||
<BootstrapInputGroupIcon Icon="fa fa-fw fa-user" />
|
||||
<BootstrapInput TValue="string" type="tel" name="phone" class="digits" maxlength="11" PlaceHolder="手机号码" />
|
||||
|
@ -28,12 +19,25 @@
|
|||
<Tooltip Title="点击发送验证码" Placement="Placement.Top" />
|
||||
</Button>
|
||||
</BootstrapInputGroup>
|
||||
</Block>
|
||||
}
|
||||
else
|
||||
{
|
||||
<BootstrapInputGroup>
|
||||
<BootstrapInputGroupIcon Icon="fa fa-fw fa-user" />
|
||||
<BootstrapInput TValue="string" name="userName" maxlength="16" IsAutoFocus="true" PlaceHolder="用户名" />
|
||||
</BootstrapInputGroup>
|
||||
<BootstrapInputGroup class="mt-3">
|
||||
<BootstrapInputGroupIcon Icon="fa fa-fw fa-lock" />
|
||||
<BootstrapPassword name="password" PlaceHolder="密码" />
|
||||
</BootstrapInputGroup>
|
||||
}
|
||||
<div class="d-flex justify-content-between mt-3">
|
||||
<Checkbox @bind-Value="RememberPassword" Color="Color.Primary" ShowAfterLabel="true" DisplayText="记住密码自动登录" />
|
||||
<SwitchButton @bind-ToggleState="AllowMobile" OffText="短信验证登录" OnText="用户密码登录" />
|
||||
<Block Condition="AllowMobile">
|
||||
<SwitchButton @bind-ToggleState="UseMobileLogin" OnClick="OnClickSwitchButton" OffText="短信验证登录" OnText="用户密码登录" />
|
||||
</Block>
|
||||
</div>
|
||||
<Button class="btn-login mt-3" ButtonType="ButtonType.Button" Size="Size.Large" IsBlock="true" Color="Color.Danger" OnClick="OnSignIn" Text="登 录">
|
||||
<Button class="btn-login mt-3" ButtonType="ButtonType.Submit" Size="Size.Large" IsBlock="true" Color="Color.Danger" Text="登 录">
|
||||
<Tooltip Placement="Placement.Top" Title="不填写密码默认使用 Gitee 认证" />
|
||||
</Button>
|
||||
<div class="d-flex justify-content-between">
|
||||
|
|
|
@ -8,31 +8,21 @@ namespace BootstrapAdmin.Web.Pages.Account
|
|||
/// </summary>
|
||||
public partial class Login
|
||||
{
|
||||
[Inject]
|
||||
[NotNull]
|
||||
private IDicts? DictsService { get; set; }
|
||||
|
||||
private string? Title { get; set; }
|
||||
|
||||
private bool AllowMobile { get; set; }
|
||||
private bool AllowMobile { get; set; } = true;
|
||||
|
||||
private bool UseMobileLogin { get; set; }
|
||||
|
||||
private bool AllowOAuth { get; set; } = true;
|
||||
|
||||
[NotNull]
|
||||
private string? UserName { get; set; }
|
||||
|
||||
[NotNull]
|
||||
private string? Password { get; set; }
|
||||
|
||||
private bool RememberPassword { get; set; }
|
||||
|
||||
[Inject]
|
||||
[NotNull]
|
||||
private NavigationManager? Navigation { get; set; }
|
||||
private string? PostUrl { get; set; } = "/Account/Login";
|
||||
|
||||
[Inject]
|
||||
[NotNull]
|
||||
private LoginService? LoginService { get; set; }
|
||||
private IDicts? DictsService { get; set; }
|
||||
|
||||
[Inject]
|
||||
[NotNull]
|
||||
|
@ -48,22 +38,9 @@ namespace BootstrapAdmin.Web.Pages.Account
|
|||
Title = DictsService.GetWebTitle();
|
||||
}
|
||||
|
||||
void OnClickMobile()
|
||||
void OnClickSwitchButton()
|
||||
{
|
||||
AllowMobile = true;
|
||||
}
|
||||
|
||||
void OnSignIn()
|
||||
{
|
||||
var auth = UserService.Authenticate(UserName, Password);
|
||||
|
||||
if (auth)
|
||||
{
|
||||
LoginService.LoginSeessionId = Guid.NewGuid().ToString();
|
||||
LoginService.UserName = UserName;
|
||||
LoginService.Remember = RememberPassword;
|
||||
Navigation.NavigateTo($"/Login?id={LoginService.LoginSeessionId}", true);
|
||||
}
|
||||
PostUrl = UseMobileLogin ? "/Account/Mobile" : "/Account/Login";
|
||||
}
|
||||
|
||||
void OnSignUp()
|
||||
|
|
Loading…
Reference in New Issue