parent
ff2237aeda
commit
566f30ed3d
@ -1,13 +0,0 @@ |
||||
<Project Sdk="Microsoft.NET.Sdk.Web"> |
||||
|
||||
<PropertyGroup> |
||||
<TargetFramework>net8.0</TargetFramework> |
||||
<Nullable>enable</Nullable> |
||||
<ImplicitUsings>enable</ImplicitUsings> |
||||
</PropertyGroup> |
||||
|
||||
<ItemGroup> |
||||
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components" Version="4.*-* " /> |
||||
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components.Icons" Version="4.*-* " /> |
||||
</ItemGroup> |
||||
</Project> |
@ -0,0 +1,65 @@ |
||||
@page "/login" |
||||
@using FluentBlazorAuth.Data |
||||
@using FluentBlazorAuth.Models.ViewModels |
||||
@using System.Security.Claims |
||||
@using Microsoft.AspNetCore.Authentication |
||||
@using Microsoft.AspNetCore.Authentication.Cookies |
||||
@inject AppDbContext AppDbContext; |
||||
@inject NavigationManager NavManager; |
||||
@rendermode InteractiveServer |
||||
|
||||
<FluentGrid Class="mt-3" Justify="JustifyContent.Center"> |
||||
<FluentGridItem xs="6"> |
||||
<FluentEditForm Model="@Model" 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)"/> |
||||
</div> |
||||
<div style="width:100%"> |
||||
<FluentTextField Style="width:100%" @bind-Value="Model.Password" TextFieldType="TextFieldType.Password" Label="Password" Required /> |
||||
<FluentValidationMessage For="@(() => Model.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> |
||||
</FluentGridItem> |
||||
</FluentGrid> |
||||
|
||||
@code { |
||||
[CascadingParameter] |
||||
public HttpContext? HttpContext { get; set; } |
||||
|
||||
[SupplyParameterFromForm] |
||||
public LoginViewModel Model { get; set; } = new LoginViewModel(); |
||||
|
||||
string? errorMessage; |
||||
|
||||
async Task OnLoginSubmit(EditContext context) |
||||
{ |
||||
var userAccount = AppDbContext.UserAccounts.Where(u => u.UserName == Model.UserName).FirstOrDefault(); |
||||
if (userAccount is null || userAccount.Password != Model.Password) |
||||
{ |
||||
errorMessage = "Please login again."; |
||||
return; |
||||
} |
||||
|
||||
var claims = new List<Claim> |
||||
{ |
||||
new Claim(ClaimTypes.Name, Model.UserName), |
||||
new Claim(ClaimTypes.Role, userAccount.Role) |
||||
}; |
||||
|
||||
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); |
||||
var principal = new ClaimsPrincipal(identity); |
||||
await HttpContext.SignInAsync(principal); |
||||
|
||||
NavManager.NavigateTo("/"); |
||||
} |
||||
} |
@ -0,0 +1,15 @@ |
||||
using FluentBlazorAuth.Models; |
||||
using Microsoft.EntityFrameworkCore; |
||||
|
||||
namespace FluentBlazorAuth.Data |
||||
{ |
||||
public class AppDbContext : DbContext |
||||
{ |
||||
public AppDbContext(DbContextOptions options) : base(options) |
||||
{ |
||||
|
||||
} |
||||
|
||||
public DbSet<UserAccount> UserAccounts { get; set; } |
||||
} |
||||
} |
@ -0,0 +1,21 @@ |
||||
<Project Sdk="Microsoft.NET.Sdk.Web"> |
||||
|
||||
<PropertyGroup> |
||||
<TargetFramework>net8.0</TargetFramework> |
||||
<Nullable>enable</Nullable> |
||||
<ImplicitUsings>enable</ImplicitUsings> |
||||
</PropertyGroup> |
||||
|
||||
<ItemGroup> |
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" /> |
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.7"> |
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> |
||||
<PrivateAssets>all</PrivateAssets> |
||||
</PackageReference> |
||||
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components" Version="4.*-* " /> |
||||
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components.Emoji" Version="4.6.0" /> |
||||
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components.Icons" Version="4.*-* " /> |
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" /> |
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.0" /> |
||||
</ItemGroup> |
||||
</Project> |
@ -0,0 +1,18 @@ |
||||
using System.ComponentModel.DataAnnotations; |
||||
|
||||
namespace FluentBlazorAuth.Models |
||||
{ |
||||
public class UserAccount |
||||
{ |
||||
public int Id { get; set; } |
||||
|
||||
[MaxLength(100)] |
||||
public required string UserName { get; set; } |
||||
|
||||
[MaxLength(100)] |
||||
public required string Password { get; set; } |
||||
|
||||
[MaxLength(20)] |
||||
public string? Role { get; set; } |
||||
} |
||||
} |
@ -0,0 +1,13 @@ |
||||
using System.ComponentModel.DataAnnotations; |
||||
|
||||
namespace FluentBlazorAuth.Models.ViewModels |
||||
{ |
||||
public class LoginViewModel |
||||
{ |
||||
[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter the user name.")] |
||||
public string? UserName { get; set; } |
||||
|
||||
[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter the password.")] |
||||
public string? Password { get; set; } |
||||
} |
||||
} |
@ -0,0 +1,66 @@ |
||||
using Microsoft.FluentUI.AspNetCore.Components; |
||||
using FluentBlazorAuth.Components; |
||||
using Microsoft.AspNetCore.Authentication.Cookies; |
||||
using FluentBlazorAuth.Data; |
||||
using Microsoft.EntityFrameworkCore; |
||||
|
||||
namespace FluentBlazorAuth; |
||||
|
||||
public class Program |
||||
{ |
||||
public static void Main(string[] args) |
||||
{ |
||||
var builder = WebApplication.CreateBuilder(args); |
||||
|
||||
// Add services to the container. |
||||
builder.Services.AddRazorComponents() |
||||
.AddInteractiveServerComponents(); |
||||
builder.Services.AddFluentUIComponents(); |
||||
|
||||
#region Authentication |
||||
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) |
||||
.AddCookie(option => |
||||
{ |
||||
option.Cookie.Name = "auth_token"; |
||||
option.LoginPath = "/login"; |
||||
option.Cookie.MaxAge = TimeSpan.FromMinutes(30); |
||||
option.AccessDeniedPath = "/access-denied"; |
||||
}); |
||||
builder.Services.AddAuthorization(); |
||||
builder.Services.AddCascadingAuthenticationState(); |
||||
#endregion |
||||
|
||||
#region Database |
||||
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); |
||||
builder.Services.AddDbContext<AppDbContext>(options => |
||||
{ |
||||
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)); |
||||
}); |
||||
#endregion |
||||
|
||||
var app = builder.Build(); |
||||
|
||||
// Configure the HTTP request pipeline. |
||||
if (!app.Environment.IsDevelopment()) |
||||
{ |
||||
app.UseExceptionHandler("/Error"); |
||||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. |
||||
app.UseHsts(); |
||||
} |
||||
|
||||
app.UseHttpsRedirection(); |
||||
|
||||
app.UseStaticFiles(); |
||||
app.UseAntiforgery(); |
||||
|
||||
#region Authentication |
||||
app.UseAuthentication(); |
||||
app.UseAuthorization(); |
||||
#endregion |
||||
|
||||
app.MapRazorComponents<App>() |
||||
.AddInteractiveServerRenderMode(); |
||||
|
||||
app.Run(); |
||||
} |
||||
} |
@ -0,0 +1,12 @@ |
||||
{ |
||||
"ConnectionStrings": { |
||||
"DefaultConnection": "Server=peacecloud.synology.me; Port=23306; Database=BLAZORTEST; Uid=pds; Pwd=Pds92070983!@" |
||||
}, |
||||
"Logging": { |
||||
"LogLevel": { |
||||
"Default": "Information", |
||||
"Microsoft.AspNetCore": "Warning" |
||||
} |
||||
}, |
||||
"AllowedHosts": "*" |
||||
} |
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
@ -1,29 +0,0 @@ |
||||
using Microsoft.FluentUI.AspNetCore.Components; |
||||
using FluentBlazorAuth.Components; |
||||
|
||||
var builder = WebApplication.CreateBuilder(args); |
||||
|
||||
// Add services to the container. |
||||
builder.Services.AddRazorComponents() |
||||
.AddInteractiveServerComponents(); |
||||
builder.Services.AddFluentUIComponents(); |
||||
|
||||
var app = builder.Build(); |
||||
|
||||
// Configure the HTTP request pipeline. |
||||
if (!app.Environment.IsDevelopment()) |
||||
{ |
||||
app.UseExceptionHandler("/Error", createScopeForErrors: true); |
||||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. |
||||
app.UseHsts(); |
||||
} |
||||
|
||||
app.UseHttpsRedirection(); |
||||
|
||||
app.UseStaticFiles(); |
||||
app.UseAntiforgery(); |
||||
|
||||
app.MapRazorComponents<App>() |
||||
.AddInteractiveServerRenderMode(); |
||||
|
||||
app.Run(); |
@ -1,9 +0,0 @@ |
||||
{ |
||||
"Logging": { |
||||
"LogLevel": { |
||||
"Default": "Information", |
||||
"Microsoft.AspNetCore": "Warning" |
||||
} |
||||
}, |
||||
"AllowedHosts": "*" |
||||
} |
Loading…
Reference in new issue