Initial Commit
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
@page "/access-denied"
|
||||
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<div class="card">
|
||||
<div class="card-body flex-column">
|
||||
<div class="text-center">
|
||||
<img src="/images/access-denied.png" style="max-height:5rem;"/>
|
||||
</div>
|
||||
<div class="text-center mt-2">
|
||||
<span class="text-danger fw-bolder">You don't have permission to access this page.</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,79 @@
|
||||
@page "/login"
|
||||
@using System.Security.Claims
|
||||
@using BlazorPolicyAuth.Data
|
||||
@using BlazorPolicyAuth.Models.ViewModels
|
||||
@using Microsoft.AspNetCore.Authentication
|
||||
@using Microsoft.AspNetCore.Authentication.Cookies
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@inject AppDbContext DbContext
|
||||
@inject NavigationManager NavigationManager
|
||||
|
||||
<div class="row">
|
||||
<div class="col-lg-4 offset-lg-4 pt-4 pb-4 border">
|
||||
<EditForm Model="Model" OnValidSubmit="Authenticate" FormName="LoginForm">
|
||||
<DataAnnotationsValidator/>
|
||||
<div class="mb-3 text-center flex-column">
|
||||
<img src="/images/login.png" style="max-height:5rem;"/>
|
||||
<h3>LOGIN</h3>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label>User Name</label>
|
||||
<InputText @bind-Value="Model.UserName" class="form-control" placeholder="Enter User Name"/>
|
||||
<ValidationMessage For="() => Model.UserName"></ValidationMessage>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label>Password</label>
|
||||
<InputText @bind-Value="Model.Password" class="form-control" placeholder="Enter Password"/>
|
||||
<ValidationMessage For="() => Model.Password"></ValidationMessage>
|
||||
</div>
|
||||
<div class="mb-3 text-center">
|
||||
<span class="text-danger">@_errorMessage</span>
|
||||
</div>
|
||||
<div class="mb-3 d-grid gap-2">
|
||||
<button class="btn btn-primary" type="submit">Login</button>
|
||||
</div>
|
||||
</EditForm>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
public HttpContext? HttpContext { get; set; }
|
||||
|
||||
[SupplyParameterFromForm]
|
||||
public LoginViewModel Model { get; set; } = new();
|
||||
|
||||
private string? _errorMessage;
|
||||
|
||||
private async Task Authenticate()
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(Model.UserName) || string.IsNullOrWhiteSpace(Model.Password))
|
||||
{
|
||||
_errorMessage = "Invalid User Name or Password";
|
||||
return;
|
||||
}
|
||||
|
||||
var userAccount = DbContext.UserAccounts.FirstOrDefault(x => x.UserName == Model.UserName);
|
||||
if (userAccount is null || userAccount.Password != Model.Password)
|
||||
{
|
||||
_errorMessage = "Invalid User Name or Password";
|
||||
return;
|
||||
}
|
||||
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new Claim(ClaimTypes.Name, Model.UserName)
|
||||
};
|
||||
|
||||
/* Add Policies */
|
||||
var userAccountPolicies = await DbContext.UserAccountPolicies.Where(x => x.UserAccountId == userAccount.Id && x.IsEnabled).ToListAsync();
|
||||
claims.AddRange(userAccountPolicies.Select(userAccountPolicy => new Claim(userAccountPolicy.UserPolicy, "true")));
|
||||
|
||||
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
await HttpContext?.SignInAsync(principal)!;
|
||||
NavigationManager.NavigateTo("/");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
@page "/logout"
|
||||
@using Microsoft.AspNetCore.Authentication
|
||||
@inject NavigationManager NavigationManager
|
||||
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<div class="card">
|
||||
<div class="card-body flex-column">
|
||||
<div class="text-center">
|
||||
<img src="/images/logout.png" style="max-height:5rem;"/>
|
||||
</div>
|
||||
<div class="text-center mt-2">
|
||||
<span class="text-danger fw-bolder">You've successfully logged out of the application.</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
public HttpContext? HttpContext { get; set; }
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await base.OnInitializedAsync();
|
||||
if (HttpContext.User.Identity.IsAuthenticated)
|
||||
{
|
||||
await HttpContext.SignOutAsync();
|
||||
|
||||
NavigationManager.NavigateTo("/logout", true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
@page "/counter"
|
||||
@rendermode InteractiveServer
|
||||
|
||||
<PageTitle>Counter</PageTitle>
|
||||
|
||||
<h1>Counter</h1>
|
||||
|
||||
<p role="status">Current count: @currentCount</p>
|
||||
|
||||
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
|
||||
|
||||
@code {
|
||||
private int currentCount = 0;
|
||||
|
||||
private void IncrementCount()
|
||||
{
|
||||
currentCount++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
@page "/Error"
|
||||
@using System.Diagnostics
|
||||
|
||||
<PageTitle>Error</PageTitle>
|
||||
|
||||
<h1 class="text-danger">Error.</h1>
|
||||
<h2 class="text-danger">An error occurred while processing your request.</h2>
|
||||
|
||||
@if (ShowRequestId)
|
||||
{
|
||||
<p>
|
||||
<strong>Request ID:</strong> <code>@RequestId</code>
|
||||
</p>
|
||||
}
|
||||
|
||||
<h3>Development Mode</h3>
|
||||
<p>
|
||||
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
|
||||
</p>
|
||||
<p>
|
||||
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
|
||||
It can result in displaying sensitive information from exceptions to end users.
|
||||
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
|
||||
and restarting the app.
|
||||
</p>
|
||||
|
||||
@code{
|
||||
[CascadingParameter]
|
||||
private HttpContext? HttpContext { get; set; }
|
||||
|
||||
private string? RequestId { get; set; }
|
||||
private bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
|
||||
|
||||
protected override void OnInitialized() =>
|
||||
RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
@page "/"
|
||||
|
||||
<PageTitle>Home</PageTitle>
|
||||
|
||||
<h1>Hello, world!</h1>
|
||||
|
||||
Welcome to your new app.
|
||||
|
||||
<AuthorizeView>
|
||||
<Authorized>
|
||||
<h4>Logged as <b>@context.User.Identity.Name</b></h4>
|
||||
</Authorized>
|
||||
</AuthorizeView>
|
||||
@@ -0,0 +1,5 @@
|
||||
@page "/not-found"
|
||||
@layout MainLayout
|
||||
|
||||
<h3>Not Found</h3>
|
||||
<p>Sorry, the content you are looking for does not exist.</p>
|
||||
@@ -0,0 +1,64 @@
|
||||
@page "/weather"
|
||||
@attribute [StreamRendering]
|
||||
|
||||
<PageTitle>Weather</PageTitle>
|
||||
|
||||
<h1>Weather</h1>
|
||||
|
||||
<p>This component demonstrates showing data.</p>
|
||||
|
||||
@if (forecasts == null)
|
||||
{
|
||||
<p><em>Loading...</em></p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th aria-label="Temperature in Celsius">Temp. (C)</th>
|
||||
<th aria-label="Temperature in Fahrenheit">Temp. (F)</th>
|
||||
<th>Summary</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var forecast in forecasts)
|
||||
{
|
||||
<tr>
|
||||
<td>@forecast.Date.ToShortDateString()</td>
|
||||
<td>@forecast.TemperatureC</td>
|
||||
<td>@forecast.TemperatureF</td>
|
||||
<td>@forecast.Summary</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
}
|
||||
|
||||
@code {
|
||||
private WeatherForecast[]? forecasts;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
// Simulate asynchronous loading to demonstrate streaming rendering
|
||||
await Task.Delay(500);
|
||||
|
||||
var startDate = DateOnly.FromDateTime(DateTime.Now);
|
||||
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
|
||||
forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = startDate.AddDays(index),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = summaries[Random.Shared.Next(summaries.Length)]
|
||||
}).ToArray();
|
||||
}
|
||||
|
||||
private class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
public int TemperatureC { get; set; }
|
||||
public string? Summary { get; set; }
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user