You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
KWWebInvApp/Services/WebAuthenticationStateProvi...

146 lines
5.2 KiB
C#

using KWWebInvApp.Data;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using System.Security.Claims;
namespace KWWebInvApp.Services
{
public class WebAuthenticationStateProvider : AuthenticationStateProvider
{
private readonly IAuthenticationService _authService;
private readonly ProtectedSessionStorage _sessionStorage;
public WebAuthenticationStateProvider(IAuthenticationService authService, ProtectedSessionStorage sessionStorage)
{
_authService = authService;
_sessionStorage = sessionStorage;
}
/*
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var user = await _authService.GetAuthenticatedUserAsync();
if (user == null)
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, user.username),
new Claim(ClaimTypes.Role, user.userlvl.ToString())
// Add any other claims based on your application's requirements
};
var identity = new ClaimsIdentity(claims, "CustomAuth");
var principal = new ClaimsPrincipal(identity);
return new AuthenticationState(principal);
}
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var principal = new ClaimsPrincipal(new ClaimsIdentity());
UserInfoServices.userinfo? user = null;
try
{
var userSessionResult = await _sessionStorage.GetAsync<UserInfoServices.userinfo>("UserInfoSession");
user = (userSessionResult.Success) ? userSessionResult.Value : null;
if (user == null)
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, user.username),
new Claim(ClaimTypes.Role, user.userlvl.ToString())
// Add any other claims based on your application's requirements
};
var identity = new ClaimsIdentity(claims, "KWWebInvAppAuth");
principal = new ClaimsPrincipal(identity);
}
catch { return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())); }
var authState = new AuthenticationState(principal);
return authState;
}
*/
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var principal = new ClaimsPrincipal(new ClaimsIdentity());
UserInfoServices.userinfo? user = null;
try
{
user = await _authService.GetAuthenticatedUserAsync();
if (user == null)
{
var userSessionResult = await _sessionStorage.GetAsync<UserInfoServices.userinfo>("UserInfoSession");
user = (userSessionResult.Success) ? userSessionResult.Value : null;
if (user == null)
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
else
await _authService.SetAuthenticatedUserAsync(user);
}
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, user.username),
new Claim(ClaimTypes.Role, user.userlvl.ToString())
// Add any other claims based on your application's requirements
};
var identity = new ClaimsIdentity(claims, "KWWebInvAppAuth");
principal = new ClaimsPrincipal(identity);
}
catch { return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())); }
return new AuthenticationState(principal);
}
public async Task<bool> LoginAsync(LoginData loginData)
{
bool loginSuccess = await _authService.LoginAsync(loginData);
if(loginSuccess)
{
UserInfoServices.userinfo user = await _authService.GetAuthenticatedUserAsync();
// Supply sessionStorage with user data first before using SetAuthenticationState.
// authState will rely on the value of UserInfoSession stored here
await _sessionStorage.SetAsync("UserInfoSession", user);
AuthenticationState authState = await GetAuthenticationStateAsync();
SetAuthenticationState(authState);
}
return loginSuccess;
}
public async Task LogoutAsync()
{
await _authService.LogoutAsync();
await _sessionStorage.DeleteAsync("UserInfoSession");
SetAuthenticationState(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
}
public void SetAuthenticationState(AuthenticationState authState)
{
NotifyAuthenticationStateChanged(Task.FromResult(authState));
}
}
}