mirror of
https://github.com/hmaxnl/SharpRSS.git
synced 2025-01-18 21:04:21 +01:00
Compare commits
No commits in common. "bd8930a75508ac12ae6d81894dda42d235ad2165" and "6e928ba4e3fdb88e40e3c7355f137d3c5a698660" have entirely different histories.
bd8930a755
...
6e928ba4e3
2
DotBased
2
DotBased
|
@ -1 +1 @@
|
||||||
Subproject commit 8531079a16df9bd10c305d22075d7a135f8f8878
|
Subproject commit d98634d8887e0bab7add7f2181c2cdd5db77e1d2
|
5
DotBased.Identity/Class1.cs
Normal file
5
DotBased.Identity/Class1.cs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
namespace DotBased.Identity;
|
||||||
|
|
||||||
|
public class Class1
|
||||||
|
{
|
||||||
|
}
|
9
DotBased.Identity/DotBased.Identity.csproj
Normal file
9
DotBased.Identity/DotBased.Identity.csproj
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
40
SharpRSS.Blazor/Auth/LocalStorageSessionStateProvider.cs
Normal file
40
SharpRSS.Blazor/Auth/LocalStorageSessionStateProvider.cs
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
using Blazored.LocalStorage;
|
||||||
|
using DotBased;
|
||||||
|
using DotBased.ASP.Auth;
|
||||||
|
using DotBased.Extensions;
|
||||||
|
using DotBased.Logging;
|
||||||
|
using Serilog;
|
||||||
|
using ILogger = DotBased.Logging.ILogger;
|
||||||
|
|
||||||
|
namespace SharpRSS.Blazor.Auth;
|
||||||
|
|
||||||
|
public class LocalStorageSessionStateProvider : ISessionStateProvider
|
||||||
|
{
|
||||||
|
public LocalStorageSessionStateProvider(ILocalStorageService localStorage)
|
||||||
|
{
|
||||||
|
_localStorage = localStorage;
|
||||||
|
_logger = LogService.RegisterLogger(typeof(LocalStorageSessionStateProvider));
|
||||||
|
}
|
||||||
|
private readonly ILocalStorageService _localStorage;
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
|
public async Task<Result<string>> GetSessionStateAsync()
|
||||||
|
{
|
||||||
|
var localStorageValue = await _localStorage.GetItemAsync<string>(ISessionStateProvider.SessionStateName);
|
||||||
|
if (localStorageValue != null && !localStorageValue.IsNullOrWhiteSpace())
|
||||||
|
return Result<string>.Ok(localStorageValue);
|
||||||
|
_logger.Warning("Failed to get session token from local storage!");
|
||||||
|
return Result<string>.Failed("Local storage returned null or empty on session token.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Result> SetSessionStateAsync(string state)
|
||||||
|
{
|
||||||
|
if (state.IsNullOrWhiteSpace())
|
||||||
|
{
|
||||||
|
Log.Warning("Tried to save empty or null session state to local storage!");
|
||||||
|
return Result.Failed("Could not set session state to local storage, value is empty or null!");
|
||||||
|
}
|
||||||
|
await _localStorage.SetItemAsync(ISessionStateProvider.SessionStateName, state);
|
||||||
|
return Result.Ok();
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,12 +12,12 @@
|
||||||
@*MudBlazor*@
|
@*MudBlazor*@
|
||||||
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
|
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
|
||||||
<link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
|
<link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
|
||||||
<HeadOutlet @rendermode="new InteractiveServerRenderMode(prerender: false)"/>
|
<HeadOutlet/>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
@*Rendermode to 'InteractiveServer' or else MudBlazor will not work with interactivity*@
|
@*Rendermode to 'InteractiveServer' or else MudBlazor will not work with interactivity*@
|
||||||
<Routes @rendermode="new InteractiveServerRenderMode(prerender: false)"/>
|
<Routes @rendermode="InteractiveServer"/>
|
||||||
<script src="_framework/blazor.web.js"></script>
|
<script src="_framework/blazor.web.js"></script>
|
||||||
@*MudBlazor*@
|
@*MudBlazor*@
|
||||||
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
|
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
|
||||||
|
|
|
@ -1,56 +1,17 @@
|
||||||
@page "/Auth/Login"
|
@page "/Auth/Login"
|
||||||
@using DotBased.ASP.Auth.Domains
|
|
||||||
@using DotBased.ASP.Auth.Services
|
|
||||||
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage
|
|
||||||
@using SharpRSS.Blazor.Extensions
|
@using SharpRSS.Blazor.Extensions
|
||||||
@using SharpRSS.Data.Domains.Auth
|
|
||||||
|
|
||||||
@inject ProtectedLocalStorage LocalStorage
|
|
||||||
@inject NavigationManager NavigationManager
|
@inject NavigationManager NavigationManager
|
||||||
@inject SecurityService SecurityService
|
|
||||||
@inject ISnackbar Snackbar
|
|
||||||
|
|
||||||
<PageTitle>Login | SharpRSS</PageTitle>
|
<PageTitle>Login | SharpRSS</PageTitle>
|
||||||
|
|
||||||
<EditForm Model="@model" OnValidSubmit="ValidSubmit">
|
|
||||||
<DataAnnotationsValidator/>
|
|
||||||
<MudGrid>
|
|
||||||
<MudItem>
|
|
||||||
<MudCard>
|
|
||||||
<MudCardContent>
|
|
||||||
<MudTextField Label="UserName" @bind-Value="model.UserName" For="@(() => model.UserName)"/>
|
|
||||||
<MudTextField Label="Password" @bind-Value="model.Password" For="@(() => model.Password)" InputType="InputType.Password"/>
|
|
||||||
</MudCardContent>
|
|
||||||
<MudCardActions>
|
|
||||||
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary">Login</MudButton>
|
|
||||||
</MudCardActions>
|
|
||||||
</MudCard>
|
|
||||||
</MudItem>
|
|
||||||
</MudGrid>
|
|
||||||
</EditForm>
|
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
|
|
||||||
private string RedirectAfterLogin { get; set; } = string.Empty;
|
private string RedirectAfterLogin { get; set; } = string.Empty;
|
||||||
LoginModel model = new();
|
|
||||||
|
|
||||||
protected override Task OnInitializedAsync()
|
protected override Task OnInitializedAsync()
|
||||||
{
|
{
|
||||||
RedirectAfterLogin = NavigationManager.GetQueryParameters().TryGetValue("RedirectUrl", out var redirectUrl) ? redirectUrl.ToString() : "/";
|
RedirectAfterLogin = NavigationManager.GetQueryParameters().TryGetValue("RedirectUrl", out var redirectUrl) ? redirectUrl.ToString() : string.Empty;
|
||||||
//TODO: Checking based auth or external (OIDC, etc.)
|
//TODO: Checking based auth or external (OIDC, etc.)
|
||||||
return base.OnInitializedAsync();
|
return base.OnInitializedAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void ValidSubmit(EditContext obj)
|
|
||||||
{
|
|
||||||
var loginResult = await SecurityService.LoginAsync(model);
|
|
||||||
if (loginResult.Success && loginResult.Value != null)
|
|
||||||
{
|
|
||||||
await LocalStorage.SetAsync("dotbased_session", loginResult.Value.Id);
|
|
||||||
NavigationManager.NavigateTo(RedirectAfterLogin);
|
|
||||||
}
|
|
||||||
|
|
||||||
Snackbar.Add(loginResult.Message, Severity.Error);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -3,11 +3,6 @@
|
||||||
<PageTitle>Home</PageTitle>
|
<PageTitle>Home</PageTitle>
|
||||||
|
|
||||||
<MudText Typo="Typo.h4">Mud text!</MudText>
|
<MudText Typo="Typo.h4">Mud text!</MudText>
|
||||||
<AuthorizeView>
|
|
||||||
<Authorized>
|
|
||||||
<span>Welcome: @context.User.Identity.Name</span>
|
|
||||||
</Authorized>
|
|
||||||
</AuthorizeView>
|
|
||||||
|
|
||||||
<AuthorizeView Roles="test">
|
<AuthorizeView Roles="test">
|
||||||
<NotAuthorized>
|
<NotAuthorized>
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
|
using Blazored.LocalStorage;
|
||||||
using DotBased.ASP.Auth;
|
using DotBased.ASP.Auth;
|
||||||
using DotBased.ASP.Auth.Domains.Auth;
|
using DotBased.ASP.Auth.Domains.Auth;
|
||||||
using DotBased.ASP.Auth.Domains.Identity;
|
using DotBased.ASP.Auth.Domains.Identity;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using MudBlazor.Services;
|
using MudBlazor.Services;
|
||||||
|
using SharpRSS.Blazor.Auth;
|
||||||
using SharpRSS.Blazor.Components;
|
using SharpRSS.Blazor.Components;
|
||||||
using SharpRSS.Business;
|
using SharpRSS.Business;
|
||||||
using SharpRSS.Data;
|
using SharpRSS.Data;
|
||||||
|
@ -11,21 +13,23 @@ using SharpRSS.Data.Domains.Configuration;
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
builder.AddSRSS();
|
builder.AddSRSS();
|
||||||
|
|
||||||
|
builder.Services.AddBlazoredLocalStorage();
|
||||||
|
|
||||||
builder.Services.AddBasedServerAuth(options =>
|
builder.Services.AddBasedServerAuth(options =>
|
||||||
{
|
{
|
||||||
options.AllowRegistration = false;
|
options.AllowRegistration = false;
|
||||||
options.AuthenticationStateMaxAgeBeforeExpire = TimeSpan.FromDays(7);
|
options.AuthenticationStateMaxAgeBeforeExpire = TimeSpan.FromDays(7);
|
||||||
options.LoginPath = "/auth/login";
|
options.LoginPath = "/auth/login";
|
||||||
options.LogoutPath = "/auth/logout";
|
options.LogoutPath = "/auth/logout";
|
||||||
options.LoggedOutPath = "/auth/loggedOut";
|
|
||||||
options.SeedData = service =>
|
options.SeedData = service =>
|
||||||
{
|
{
|
||||||
service.CreateUserAsync(new UserModel() { UserName = "Admin", Name = "Administrator", FamilyName = "admin", Email = "admin@example.com", Enabled = true, PasswordHash = "password",
|
service.CreateUserAsync(new UserModel() { UserName = "Admin", Email = "admin@example.com", Enabled = true, PasswordHash = "password", Roles =
|
||||||
Roles = [new RoleModel { Name = "Admin", Description = "Administration role." }]
|
[new RoleModel { Name = "Admin", Description = "Administration role." }]
|
||||||
});
|
});
|
||||||
service.CreateUserAsync(new UserModel() { UserName = "User", Email = "user@example.com", Enabled = true, PasswordHash = "password"});
|
service.CreateUserAsync(new UserModel() { UserName = "User", Email = "user@example.com", Enabled = true, PasswordHash = "password"});
|
||||||
};
|
};
|
||||||
options.SetDataRepositoryType<MemoryAuthDataRepository>();
|
options.SetDataRepositoryType<MemoryAuthDataRepository>();
|
||||||
|
options.SetSessionStateProviderType<LocalStorageSessionStateProvider>();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
|
||||||
<PackageReference Include="MudBlazor" Version="6.20.0" />
|
<PackageReference Include="MudBlazor" Version="6.20.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -30,8 +31,4 @@
|
||||||
</Compile>
|
</Compile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Auth\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
using DotBased;
|
using DotBased;
|
||||||
using DotBased.ASP.Auth.Domains;
|
|
||||||
using DotBased.Logging;
|
using DotBased.Logging;
|
||||||
using SharpRSS.Data.Domains.Auth;
|
using SharpRSS.Data.Domains.Auth;
|
||||||
|
|
||||||
|
|
7
SharpRSS.Data/Domains/Auth/LoginModel.cs
Normal file
7
SharpRSS.Data/Domains/Auth/LoginModel.cs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
namespace SharpRSS.Data.Domains.Auth;
|
||||||
|
|
||||||
|
public class LoginModel
|
||||||
|
{
|
||||||
|
public string UserName { get; set; } = string.Empty;
|
||||||
|
public string Password { get; set; } = string.Empty;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user