Добавлена аутентификаця фейковая РАБОТАЕТ!!!
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
<Router AppAssembly="@typeof(App).Assembly">
|
<CascadingAuthenticationState>
|
||||||
|
<Router AppAssembly="@typeof(App).Assembly">
|
||||||
<Found Context="routeData">
|
<Found Context="routeData">
|
||||||
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
|
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
|
||||||
|
|
||||||
|
@*</AuthorizeRouteView>*@
|
||||||
</Found>
|
</Found>
|
||||||
<NotFound>
|
<NotFound>
|
||||||
<PageTitle>Not found</PageTitle>
|
<PageTitle>Not found</PageTitle>
|
||||||
@@ -8,4 +11,5 @@
|
|||||||
<p role="alert">Sorry, there's nothing at this address.</p>
|
<p role="alert">Sorry, there's nothing at this address.</p>
|
||||||
</LayoutView>
|
</LayoutView>
|
||||||
</NotFound>
|
</NotFound>
|
||||||
</Router>
|
</Router>
|
||||||
|
</CascadingAuthenticationState>
|
||||||
|
|||||||
@@ -2,6 +2,43 @@
|
|||||||
|
|
||||||
<PageTitle>Index</PageTitle>
|
<PageTitle>Index</PageTitle>
|
||||||
|
|
||||||
<MudText Typo="Typo.h3" GutterBottom="true">Hello, world!</MudText>
|
@*<MudText Typo="Typo.h3" GutterBottom="true">Hello, world!</MudText>
|
||||||
<MudText Class="mb-8">Welcome to your new app, powered by MudBlazor!</MudText>
|
<MudText Class="mb-8">Welcome to your new app, powered by MudBlazor!</MudText>
|
||||||
<MudAlert Severity="Severity.Normal">You can find documentation and examples on our website here: <MudLink Href="https://mudblazor.com" Typo="Typo.body2" Color="Color.Inherit"><b>www.mudblazor.com</b></MudLink></MudAlert>
|
<MudAlert Severity="Severity.Normal">You can find documentation and examples on our website here: <MudLink Href="https://mudblazor.com" Typo="Typo.body2" Color="Color.Inherit"><b>www.mudblazor.com</b></MudLink></MudAlert>
|
||||||
|
*@
|
||||||
|
<AuthorizeView>
|
||||||
|
<Authorized>
|
||||||
|
<MudText Typo="Typo.h3" GutterBottom="true">Hello, world!</MudText>
|
||||||
|
<b>Hello, @context.User.Identity.Name!</b>
|
||||||
|
<p>@authMessage</p>
|
||||||
|
</Authorized>
|
||||||
|
<NotAuthorized>
|
||||||
|
<p>@authMessage</p>
|
||||||
|
<b>Hello, @context.User.Identity.Name!</b>
|
||||||
|
@* <MudText Class="mb-8">Welcome to your new app, powered by MudBlazor!</MudText>
|
||||||
|
<MudAlert Severity="Severity.Normal">You can find documentation and examples on our website here: <MudLink Href="https://mudblazor.com" Typo="Typo.body2" Color="Color.Inherit"><b>www.mudblazor.com</b></MudLink></MudAlert>*@
|
||||||
|
</NotAuthorized>
|
||||||
|
|
||||||
|
</AuthorizeView>
|
||||||
|
|
||||||
|
|
||||||
|
@code {
|
||||||
|
private string authMessage = "The user is NOT authenticated.";
|
||||||
|
|
||||||
|
[CascadingParameter]
|
||||||
|
private Task<AuthenticationState>? authenticationState { get; set; }
|
||||||
|
|
||||||
|
protected override async Task OnInitializedAsync()
|
||||||
|
{
|
||||||
|
if (authenticationState is not null)
|
||||||
|
{
|
||||||
|
var authState = await authenticationState;
|
||||||
|
var user = authState?.User;
|
||||||
|
|
||||||
|
if (user?.Identity is not null && user.Identity.IsAuthenticated)
|
||||||
|
{
|
||||||
|
authMessage = $"{user.Identity.Name} is authenticated.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,49 +30,13 @@ namespace BlazorCookieAuth.Server.Pages
|
|||||||
if (ModelState.IsValid)
|
if (ModelState.IsValid)
|
||||||
{
|
{
|
||||||
ClaimsPrincipal claimsPrincipal = _biudAuthService.AuthenticateUser(paramUsername, paramPassword);
|
ClaimsPrincipal claimsPrincipal = _biudAuthService.AuthenticateUser(paramUsername, paramPassword);
|
||||||
|
|
||||||
|
_biudAuthService.Users.Add(paramUsername,claimsPrincipal);
|
||||||
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal);
|
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal);
|
||||||
|
return LocalRedirect(returnUrl);
|
||||||
}
|
}
|
||||||
|
return Page();
|
||||||
|
|
||||||
//try
|
|
||||||
//{
|
|
||||||
// // Clear the existing external cookie
|
|
||||||
// await HttpContext
|
|
||||||
// .SignOutAsync(
|
|
||||||
// CookieAuthenticationDefaults.AuthenticationScheme);
|
|
||||||
//}
|
|
||||||
//catch { }
|
|
||||||
|
|
||||||
//// *** !!! This is where you would validate the user !!! ***
|
|
||||||
//// In this example we just log the user in
|
|
||||||
//// (Always log the user in for this demo)
|
|
||||||
|
|
||||||
//var claims = new List<Claim>
|
|
||||||
//{
|
|
||||||
// new Claim(ClaimTypes.Name, paramUsername),
|
|
||||||
// new Claim(ClaimTypes.Role, "Administrator"),
|
|
||||||
//};
|
|
||||||
|
|
||||||
//var claimsIdentity = new ClaimsIdentity(
|
|
||||||
// claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
|
||||||
|
|
||||||
//var authProperties = new AuthenticationProperties
|
|
||||||
//{
|
|
||||||
// IsPersistent = true,
|
|
||||||
// RedirectUri = this.Request.Host.Value
|
|
||||||
//};
|
|
||||||
|
|
||||||
//try
|
|
||||||
//{
|
|
||||||
// await HttpContext.SignInAsync(
|
|
||||||
// CookieAuthenticationDefaults.AuthenticationScheme,
|
|
||||||
// new ClaimsPrincipal(claimsIdentity),
|
|
||||||
// authProperties);
|
|
||||||
//}
|
|
||||||
//catch (Exception ex)
|
|
||||||
//{
|
|
||||||
// string error = ex.Message;
|
|
||||||
//}
|
|
||||||
return LocalRedirect(returnUrl);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,6 @@
|
|||||||
using Microsoft.AspNetCore.Components;
|
using Microsoft.AspNetCore.Components;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using Microsoft.AspNetCore.Authentication;
|
|
||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
||||||
using TestApp02.Data;
|
using TestApp02.Data;
|
||||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
||||||
using System.Security.Claims;
|
|
||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
||||||
using System.Web;
|
using System.Web;
|
||||||
|
|
||||||
namespace TestApp02.Pages
|
namespace TestApp02.Pages
|
||||||
@@ -29,15 +24,6 @@ namespace TestApp02.Pages
|
|||||||
|
|
||||||
protected async Task OnPostAsync()
|
protected async Task OnPostAsync()
|
||||||
{
|
{
|
||||||
|
|
||||||
//ClaimsPrincipal principal;
|
|
||||||
//principal = _BiudAuthService.AuthenticateUser(loginAccountForm.Username, loginAccountForm.Password);
|
|
||||||
|
|
||||||
//if (principal == null) {
|
|
||||||
// //await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
|
|
||||||
//};
|
|
||||||
|
|
||||||
//var identity = principal.Identity as ClaimsIdentity;
|
|
||||||
string _uName = encode(loginAccountForm.Username);
|
string _uName = encode(loginAccountForm.Username);
|
||||||
string _uPass = encode(loginAccountForm.Password);
|
string _uPass = encode(loginAccountForm.Password);
|
||||||
|
|
||||||
|
|||||||
@@ -12,13 +12,12 @@ StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configurat
|
|||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddRazorPages();
|
builder.Services.AddRazorPages();
|
||||||
builder.Services.AddServerSideBlazor();
|
builder.Services.AddServerSideBlazor();
|
||||||
|
builder.Services.AddMudServices();
|
||||||
|
|
||||||
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
|
||||||
.AddCookie();
|
|
||||||
builder.Services.AddSingleton<BiudAuthService>();
|
builder.Services.AddSingleton<BiudAuthService>();
|
||||||
|
|
||||||
builder.Services.AddSingleton<WeatherForecastService>();
|
builder.Services.AddSingleton<WeatherForecastService>();
|
||||||
builder.Services.AddMudServices();
|
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
@@ -31,7 +30,8 @@ if (!app.Environment.IsDevelopment())
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
app.UseAuthentication();
|
||||||
|
app.UseAuthorization();
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
|
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
|
|||||||
Reference in New Issue
Block a user