[REFACTOR] Refactored logging & added support for Microsoft.Extensions.Logging. Added test WebApi project
This commit is contained in:
@@ -18,21 +18,23 @@ public class BasedServerAuthenticationStateProvider : ServerAuthenticationStateP
|
||||
_config = configuration;
|
||||
_localStorage = localStorage;
|
||||
_securityService = securityService;
|
||||
_logger = LogService.RegisterLogger(typeof(BasedServerAuthenticationStateProvider));
|
||||
_logger = LogService.RegisterLogger<BasedServerAuthenticationStateProvider>();
|
||||
}
|
||||
|
||||
private BasedAuthConfiguration _config;
|
||||
private readonly ProtectedLocalStorage _localStorage;
|
||||
private readonly SecurityService _securityService;
|
||||
private ILogger _logger;
|
||||
private readonly ILogger _logger;
|
||||
private readonly AuthenticationState _anonState = new(new ClaimsPrincipal());
|
||||
|
||||
|
||||
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||
{
|
||||
_logger.Debug("Getting authentication state...");
|
||||
var sessionIdResult = await _localStorage.GetAsync<string>(BasedAuthDefaults.StorageKey);
|
||||
if (!sessionIdResult.Success || sessionIdResult.Value == null)
|
||||
return _anonState;
|
||||
_logger.Debug("Found state [{State}], getting session from {Service}", sessionIdResult.Value, nameof(SecurityService));
|
||||
var stateResult = await _securityService.GetAuthenticationStateFromSessionAsync(sessionIdResult.Value);
|
||||
return stateResult is { Success: true, Value: not null } ? stateResult.Value : _anonState;
|
||||
}
|
||||
|
@@ -18,6 +18,7 @@ public static class DotBasedAuthDependencyInjection
|
||||
var Configuration = new BasedAuthConfiguration();
|
||||
configurationAction?.Invoke(Configuration);
|
||||
|
||||
|
||||
services.AddSingleton<BasedAuthConfiguration>(Configuration);
|
||||
if (Configuration.AuthDataRepositoryType == null)
|
||||
throw new ArgumentNullException(nameof(Configuration.AuthDataRepositoryType), $"No '{nameof(IAuthDataRepository)}' configured!");
|
||||
|
@@ -34,11 +34,11 @@ public class MemoryAuthDataRepository : IAuthDataRepository
|
||||
public async Task<Result<UserModel>> GetUserAsync(string id, string email, string username)
|
||||
{
|
||||
UserModel? userModel = null;
|
||||
if (!id.IsNullOrWhiteSpace())
|
||||
if (!id.IsNullOrEmpty())
|
||||
userModel = MemoryData.users.FirstOrDefault(u => u.Id.Equals(id, StringComparison.OrdinalIgnoreCase));
|
||||
if (!email.IsNullOrWhiteSpace())
|
||||
if (!email.IsNullOrEmpty())
|
||||
userModel = MemoryData.users.FirstOrDefault(u => u.Email.Equals(email, StringComparison.OrdinalIgnoreCase));
|
||||
if (!username.IsNullOrWhiteSpace())
|
||||
if (!username.IsNullOrEmpty())
|
||||
userModel = MemoryData.users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.OrdinalIgnoreCase));
|
||||
return userModel != null ? Result<UserModel>.Ok(userModel) : Result<UserModel>.Failed("No user found!");
|
||||
}
|
||||
|
@@ -16,7 +16,7 @@ public class SecurityService
|
||||
_authDataRepository = authDataRepository;
|
||||
_dataCache = dataCache;
|
||||
_localStorage = localStorage;
|
||||
_logger = LogService.RegisterLogger(typeof(SecurityService));
|
||||
_logger = LogService.RegisterLogger<SecurityService>();
|
||||
}
|
||||
|
||||
private readonly IAuthDataRepository _authDataRepository;
|
||||
@@ -26,7 +26,7 @@ public class SecurityService
|
||||
|
||||
public async Task<Result<AuthenticationState>> GetAuthenticationStateFromSessionAsync(string id)
|
||||
{
|
||||
if (id.IsNullOrWhiteSpace())
|
||||
if (id.IsNullOrEmpty())
|
||||
return Result<AuthenticationState>.Failed("No valid id!");
|
||||
AuthenticationStateModel? authStateModel = null;
|
||||
var stateCache = _dataCache.RequestSessionState(id);
|
||||
@@ -75,13 +75,13 @@ public class SecurityService
|
||||
{
|
||||
UserModel? user = null;
|
||||
Result<UserModel> usrResult;
|
||||
if (!login.UserName.IsNullOrWhiteSpace())
|
||||
if (!login.UserName.IsNullOrEmpty())
|
||||
{
|
||||
usrResult = await _authDataRepository.GetUserAsync(string.Empty, string.Empty, login.UserName);
|
||||
if (usrResult is { Success: true, Value: not null })
|
||||
user = usrResult.Value;
|
||||
}
|
||||
else if (!login.Email.IsNullOrWhiteSpace())
|
||||
else if (!login.Email.IsNullOrEmpty())
|
||||
{
|
||||
usrResult = await _authDataRepository.GetUserAsync(string.Empty, login.Email, string.Empty);
|
||||
if (usrResult is { Success: true, Value: not null })
|
||||
@@ -114,7 +114,7 @@ public class SecurityService
|
||||
{
|
||||
try
|
||||
{
|
||||
if (state.IsNullOrWhiteSpace())
|
||||
if (state.IsNullOrEmpty())
|
||||
return Result.Failed($"Argument {nameof(state)} is empty!");
|
||||
|
||||
var stateResult = await _authDataRepository.GetAuthenticationStateAsync(state);
|
||||
|
Reference in New Issue
Block a user