tagger/Tagger/Service/UserManager.cs
2024-09-22 16:57:48 +02:00

167 lines
4.0 KiB
C#

using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Authentication;
using System.Net.NetworkInformation;
using Tagger.Pages;
using System.Security.Claims;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using System.ComponentModel;
namespace Tagger.Service;
public class UserManager
{
Configuration Conf;
ProtectedLocalStorage ProtectedLocalStorage;
public UserManager(IConfiguration configuration, ProtectedLocalStorage storage)
{
Conf = configuration.Get<Configuration>() ?? new();
ProtectedLocalStorage = storage;
}
public async Task<User?> CurrentUser()
{
ProtectedBrowserStorageResult<User> ret;
try
{
ret = await ProtectedLocalStorage.GetAsync<User>("logged");
}
catch // statically rendered, not logged :)
{
return null;
}
if (!ret.Success)
return null;
return ret.Value;
}
public async Task<bool> IsLogged()
{
return await CurrentUser() is not null;
}
public async Task Logout()
{
await ProtectedLocalStorage.DeleteAsync("logged");
}
public async Task Login(User user)
{
await ProtectedLocalStorage.SetAsync("logged", user);
}
public async Task<bool> TryLogin(string username, string password)
{
await Logout();
foreach (var user in Conf.Users)
if (user.Name.ToLower() == username.ToLower())
{
if (user.Password != password)
return false;
await Login(user);
return true;
}
return false;
}
/*public bool IsLogged(HttpContext state)
{
if (state.User is null)
return false;
if (state.User.Identity is null)
return false;
return state.User.Identity.IsAuthenticated;
}
public async Task<bool> IsLogged(AuthenticationStateProvider state)
{
AuthenticationState authState = await state.GetAuthenticationStateAsync();
if (authState.User is null)
return false;
if (authState.User.Identity is null)
return false;
return authState.User.Identity.IsAuthenticated;
}
public async Task<bool> TryLogin(HttpContext state, string username, string password)
{
//await Logout(state);
foreach(var user in Conf.Users)
if(user.Name.ToLower() == username.ToLower())
{
if (user.Password != password)
return false;
await Login(state, user);
return true;
}
return false;
}
public async Task<bool> TryLogin(IHostEnvironmentAuthenticationStateProvider state, string username, string password)
{
//todo: Logout
foreach (var user in Conf.Users)
if (user.Name.ToLower() == username.ToLower())
{
if (user.Password != password)
return false;
await Login(state, user);
return true;
}
return false;
}
public async Task Login(IHostEnvironmentAuthenticationStateProvider state, User user)
{
var claims = new ClaimsPrincipal(new ClaimsIdentity[]
{
new ClaimsIdentity(new Claim[]
{
new(ClaimTypes.Name, user.Name)
})
});
state.SetAuthenticationState(
Task.FromResult(
new AuthenticationState(claims)
)
);
}
private async Task Login(HttpContext state, User user)
{
await state.SignInAsync( new ClaimsPrincipal(new ClaimsIdentity[]
{
new ClaimsIdentity(new Claim[]
{
new(ClaimTypes.Name, user.Name)
})
}));
}
public async Task Logout(HttpContext state)
{
if (IsLogged(state))
await state.SignOutAsync();
}*/
}