Код: Выделить всё
using BlazorWebApp.Components;
using BlazorWebApp.Services;
using BLL_Shared.Models.Configurations;
using DAL.Abstractions;
using DAL.Implementations;
using DAL.Models;
using DevExpress.Blazor;
using LocalizationServices.Helpers;
using LocalizationServices.Implementations;
using LocalizationServices.Abstractions;
using StorageServices.Abstractions;
using StorageServices.Helpers;
using StorageServices.Implementations;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using Microsoft.JSInterop;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Authentication.Cookies;
namespace BlazorWebApp;
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.AddDevExpressBlazor(configure => configure.BootstrapVersion = BootstrapVersion.v5);
builder.Configuration.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json");
//builder.Services.Configure(builder.Configuration.GetSection("DefaultConnection").Bind());
builder.Services.AddSingleton(p => new ConnectionModel()
{
ConnectionString = builder.Configuration.GetConnectionString("DefaultConnection")
?? throw new Exception("Connection string was not found!")
});
builder.Services.AddTransient();
builder.Services.AddTransient();
builder.Services.AddTransient();
//builder.Logging.AddFilter();
builder.Services.UseStorages(new(StoragePath: "configurations.db"));
// todo: investigate why next DI logic is not working as expected
//builder.Services.AddTransient(provider => provider.GetService(typeof(IStorageService)) as IStorageService);
//builder.Services.AddTransient(typeof(IStorageService), typeof(StorageService));
builder.Services.AddSingleton();
builder.Services.AddSingleton();
builder.Services.AddScoped();
// Add Custom Authentication State Provider and ProtectedSessionStorage
builder.Services.AddAuthorizationCore();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.Cookie.Name = "token_auth";
options.LoginPath = "/login";
options.Cookie.MaxAge = TimeSpan.FromHours(3); //To be determined
options.AccessDeniedPath = "/access-denied";
});
builder.Services.AddAuthorization();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddSingleton();
builder.Services.UseLocalization(new(
StoragePath: "localization.db",
SourceLocalization: "EN",
InitialLocalization: "EN")
);
builder.Services.AddBlazorBootstrap();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseAntiforgery();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorComponents()
.AddInteractiveServerRenderMode();
app.Run();
}
}
Код: Выделить всё
@page "/login"
@using BlazorWebApp.Services
@using System.Security.Claims
@using Microsoft.AspNetCore.Authentication
@using Microsoft.AspNetCore.Authentication.Cookies
@inject AuthentificationSerice AuthService
@inject NavigationManager Navigation
@inject IHttpContextAccessor HttpContextAccessor
Login
@if(!string.IsNullOrEmpty(ErrorMessage))
{
@ErrorMessage
}
Username
@if(!IsSimpleUser)
{
Password:
}
Simple User(No Password Required)
Login
@code {
private string Username { get; set; }
public string Password { get; set; }
private bool IsSimpleUser { get; set; } = true;
private string ErrorMessage{ get; set; }
private string UserJson { get; set; }
private async Task LogIn()
{
var response = await AuthService.Login(Username, IsSimpleUser ? null : Password);
if(response != null)
{
UserJson = System.Text.Json.JsonSerializer.Serialize(response, new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
var claims = new List
{
new Claim(ClaimTypes.Name, response.Username),
new Claim(ClaimTypes.Role, response.Role)
};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);
var httpContext = HttpContextAccessor.HttpContext;
await httpContext.SignInAsync(principal);
Navigation.NavigateTo("/");
}
else
{
ErrorMessage = "Invalid username or password";
}
}
}

Я также пробовал использовать CustomAuthenticationStateProvider, но проблема заключалась в том, что после того, как пользователь вошел в систему, я автоматически перенаправлялся на авторизованную страницу просмотра, и все было в порядке, он мог видеть авторизованный контент, но когда он обновил страницу или перешел на другую страницу, а затем обратно на авторизованную страницу, он больше не мог видеть авторизованный контент.
Теперь я также пытался использовать локальное хранилище, используя JSRunetime и SecuredStorage от Microsoft, но у меня слишком много ошибок на них. и теперь я пытаюсь использовать токены cookie. Пожалуйста, помогите, я действительно запутался.
Подробнее здесь: https://stackoverflow.com/questions/790 ... ng-cookies
Мобильная версия