47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using System.Security.Claims;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BLAIzor.Services
|
|
{
|
|
public class CustomAuthenticationStateProvider : AuthenticationStateProvider
|
|
{
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
public CustomAuthenticationStateProvider(IHttpContextAccessor httpContextAccessor)
|
|
{
|
|
_httpContextAccessor = httpContextAccessor;
|
|
}
|
|
|
|
public override Task<AuthenticationState> GetAuthenticationStateAsync()
|
|
{
|
|
var user = _httpContextAccessor.HttpContext?.User;
|
|
|
|
if (user == null || !user.Identity.IsAuthenticated)
|
|
{
|
|
var anonymous = new ClaimsPrincipal(new ClaimsIdentity());
|
|
return Task.FromResult(new AuthenticationState(anonymous));
|
|
}
|
|
|
|
return Task.FromResult(new AuthenticationState(user));
|
|
}
|
|
|
|
public string GetUserId()
|
|
{
|
|
var user = _httpContextAccessor.HttpContext?.User;
|
|
return user?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
|
}
|
|
|
|
public string GetUserName()
|
|
{
|
|
var user = _httpContextAccessor.HttpContext?.User;
|
|
return user?.Identity?.Name;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|