|
|
|
@ -4,31 +4,38 @@ |
|
|
|
|
@using System.Security.Claims |
|
|
|
|
@using Microsoft.AspNetCore.Authentication |
|
|
|
|
@using Microsoft.AspNetCore.Authentication.Cookies |
|
|
|
|
@inject AppDbContext AppDbContext; |
|
|
|
|
@inject NavigationManager NavManager; |
|
|
|
|
@rendermode InteractiveServer |
|
|
|
|
@inject AppDbContext AppDbContext |
|
|
|
|
@inject NavigationManager NavManager |
|
|
|
|
|
|
|
|
|
<FluentGrid Class="mt-3" Justify="JustifyContent.Center"> |
|
|
|
|
<FluentGridItem xs="6"> |
|
|
|
|
<FluentEditForm Model="@Model" OnValidSubmit="@OnLoginSubmit"> |
|
|
|
|
<DataAnnotationsValidator/> |
|
|
|
|
<FluentValidationSummary/> |
|
|
|
|
<FluentGridItem xs="9" md="6" lg="3"> |
|
|
|
|
<FluentCard> |
|
|
|
|
<FluentStack Orientation="Orientation.Vertical" HorizontalAlignment="HorizontalAlignment.Center"> |
|
|
|
|
<FluentEmoji Value="@(new Emojis.SmileysEmotion.Color.Default.GrinningFaceWithSmilingEyes())" Width="50px" /> |
|
|
|
|
<FluentLabel Typo="Typography.H2"> |
|
|
|
|
Login |
|
|
|
|
</FluentLabel> |
|
|
|
|
<EditForm FormName="LoginForm" style="width:100%" Model="@LoginViewModel" OnValidSubmit="@OnLoginSubmit"> |
|
|
|
|
<DataAnnotationsValidator /> |
|
|
|
|
<FluentValidationSummary /> |
|
|
|
|
|
|
|
|
|
<FluentStack Orientation="Orientation.Vertical" VerticalGap="5" HorizontalAlignment="HorizontalAlignment.Center"> |
|
|
|
|
<div style="width:100%"> |
|
|
|
|
<FluentTextField style="width:100%" @bind-Value="Model.UserName" Label="User Name" Required /> |
|
|
|
|
<FluentValidationMessage For="@(() => Model.UserName)"/> |
|
|
|
|
<FluentTextField style="width:100%" Name="username" @bind-Value="LoginViewModel.UserName" Label="User Name" Required /> |
|
|
|
|
<FluentValidationMessage For="@(() => LoginViewModel.UserName)" /> |
|
|
|
|
</div> |
|
|
|
|
<div style="width:100%"> |
|
|
|
|
<FluentTextField Style="width:100%" @bind-Value="Model.Password" TextFieldType="TextFieldType.Password" Label="Password" Required /> |
|
|
|
|
<FluentValidationMessage For="@(() => Model.Password)" /> |
|
|
|
|
<FluentTextField Style="width:100%" Name="password" @bind-Value="LoginViewModel.Password" TextFieldType="TextFieldType.Password" Label="Password" Required /> |
|
|
|
|
<FluentValidationMessage For="@(() => LoginViewModel.Password)" /> |
|
|
|
|
</div> |
|
|
|
|
<div style="width:100%"> |
|
|
|
|
<FluentLabel Color="Color.Error">@errorMessage?.ToString()</FluentLabel> |
|
|
|
|
</div> |
|
|
|
|
<FluentButton Style="width:100%" Type="ButtonType.Submit" Appearance="Appearance.Accent">Login</FluentButton> |
|
|
|
|
</FluentStack> |
|
|
|
|
</FluentEditForm> |
|
|
|
|
</EditForm> |
|
|
|
|
</FluentStack> |
|
|
|
|
</FluentCard> |
|
|
|
|
</FluentGridItem> |
|
|
|
|
</FluentGrid> |
|
|
|
|
|
|
|
|
@ -37,22 +44,29 @@ |
|
|
|
|
public HttpContext? HttpContext { get; set; } |
|
|
|
|
|
|
|
|
|
[SupplyParameterFromForm] |
|
|
|
|
public LoginViewModel Model { get; set; } = new LoginViewModel(); |
|
|
|
|
public LoginViewModel LoginViewModel { get; set; } |
|
|
|
|
|
|
|
|
|
string? errorMessage; |
|
|
|
|
|
|
|
|
|
protected override void OnInitialized() |
|
|
|
|
{ |
|
|
|
|
base.OnInitialized(); |
|
|
|
|
LoginViewModel = new LoginViewModel(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async Task OnLoginSubmit(EditContext context) |
|
|
|
|
{ |
|
|
|
|
var userAccount = AppDbContext.UserAccounts.Where(u => u.UserName == Model.UserName).FirstOrDefault(); |
|
|
|
|
if (userAccount is null || userAccount.Password != Model.Password) |
|
|
|
|
var userAccount = AppDbContext.UserAccounts.Where(u => u.UserName == LoginViewModel.UserName).FirstOrDefault(); |
|
|
|
|
if (userAccount is null || userAccount.Password != LoginViewModel.Password) |
|
|
|
|
{ |
|
|
|
|
errorMessage = "Please login again."; |
|
|
|
|
await InvokeAsync(StateHasChanged); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var claims = new List<Claim> |
|
|
|
|
{ |
|
|
|
|
new Claim(ClaimTypes.Name, Model.UserName), |
|
|
|
|
new Claim(ClaimTypes.Name, LoginViewModel.UserName), |
|
|
|
|
new Claim(ClaimTypes.Role, userAccount.Role) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|