packages clean up
This commit is contained in:
@@ -0,0 +1 @@
|
||||
@page "/identity/login"
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace LaaProductionWeb.Blazor.Components.Identity.Models
|
||||
{
|
||||
public class IdentitiyRoles
|
||||
{
|
||||
public const string HeliumTester = nameof(HeliumTester);
|
||||
public const string JustUser = nameof(JustUser);
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
namespace LaaProductionWeb.Blazor.Components.Identity.Services
|
||||
{
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
|
||||
public class IdentityJwtService(ILogger<IdentityJwtService> logger)
|
||||
{
|
||||
private readonly ILogger<IdentityJwtService> logger = logger;
|
||||
|
||||
public IEnumerable<Claim> GetClaims(string token)
|
||||
{
|
||||
var claims = new List<Claim>();
|
||||
|
||||
try
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(token))
|
||||
{
|
||||
var jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
|
||||
var securityToken = jwtSecurityTokenHandler.ReadToken(token);
|
||||
var jwtSecurityToken = securityToken as JwtSecurityToken;
|
||||
|
||||
if (jwtSecurityToken.ValidTo <= DateTime.UtcNow)
|
||||
{
|
||||
claims.AddRange(jwtSecurityToken.Claims);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.logger.LogError(e, e.Message);
|
||||
}
|
||||
|
||||
if (claims.Count == 0)
|
||||
{
|
||||
claims.Add(new Claim(ClaimTypes.Role, "JustUser"));
|
||||
}
|
||||
|
||||
return claims;
|
||||
}
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
namespace LaaProductionWeb.Blazor.Components.Identity.Services
|
||||
{
|
||||
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class IdentityService(ProtectedLocalStorage protectedLocalStorage, IdentityJwtService jwtService, ILogger<IdentityService> logger)
|
||||
{
|
||||
private readonly ProtectedLocalStorage protectedLocalStorage = protectedLocalStorage;
|
||||
private readonly IdentityJwtService jwtService = jwtService;
|
||||
private readonly ILogger<IdentityService> logger = logger;
|
||||
|
||||
public async Task<ClaimsPrincipal> GetClaimsPrincipalAsync()
|
||||
{
|
||||
var claimsIdentity = new ClaimsIdentity();
|
||||
|
||||
try
|
||||
{
|
||||
var tokenResult = await this.protectedLocalStorage
|
||||
.GetAsync<string>(typeof(Program).Namespace, "JWT");
|
||||
var token = tokenResult.Success ? tokenResult.Value : default;
|
||||
var claims = this.jwtService.GetClaims(token);
|
||||
|
||||
if (claims.Any())
|
||||
{
|
||||
claimsIdentity = new ClaimsIdentity(
|
||||
claims: claims
|
||||
, authenticationType: ClaimTypes.NameIdentifier
|
||||
, nameType: ClaimTypes.Name
|
||||
, roleType: ClaimTypes.Role);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.logger.LogError(e, e.Message);
|
||||
}
|
||||
|
||||
return new ClaimsPrincipal(claimsIdentity);
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
namespace LaaProductionWeb.Blazor.Components.Identity.Services
|
||||
{
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class IdentityStateProvider(IdentityService identityService): AuthenticationStateProvider
|
||||
{
|
||||
private readonly IdentityService identityService = identityService;
|
||||
|
||||
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||
{
|
||||
var claimsPrincipal = await this.identityService.GetClaimsPrincipalAsync();
|
||||
|
||||
return new AuthenticationState(claimsPrincipal);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user