Добавлена аутентификаця фейковая РАБОТАЕТ!!!

master
084MochalinPA 2 years ago
parent 29381c25fa
commit 677fc4a1a4

@ -1,6 +1,9 @@
<Router AppAssembly="@typeof(App).Assembly">
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
@*</AuthorizeRouteView>*@
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
@ -9,3 +12,4 @@
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>

@ -2,6 +2,43 @@
<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>
<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)
{
ClaimsPrincipal claimsPrincipal = _biudAuthService.AuthenticateUser(paramUsername, paramPassword);
_biudAuthService.Users.Add(paramUsername,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 System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using TestApp02.Data;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.Cookies;
using System.Web;
namespace TestApp02.Pages
@ -29,15 +24,6 @@ namespace TestApp02.Pages
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 _uPass = encode(loginAccountForm.Password);

@ -12,13 +12,12 @@ StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configurat
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddMudServices();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
builder.Services.AddSingleton<BiudAuthService>();
builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddMudServices();
var app = builder.Build();
@ -31,7 +30,8 @@ if (!app.Environment.IsDevelopment())
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseStaticFiles();
app.UseRouting();

Loading…
Cancel
Save