/
onimor
/
ASWRemoteViewerRev
Обзор
Документация
Войти
/
onimor
/
ASWRemoteViewerRev
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
RemoteViewing/ASW.RemoteViewing/ASW.RemoteViewing.Client/Services/UserContext/UserContextService.cs
66 строк
2 KB
onimor
Добавьте файлы проекта.
28 авг 2025, 13:00
28 авг 2025, 13:00
855ae9a
Код
Авторство
О чём код?
using Microsoft.AspNetCore.Components.Authorization; using System.Security.Claims; namespace ASW.RemoteViewing.Client.Services.UserContext; public class UserContextService : IUserContextService { private readonly AuthenticationStateProvider _authProvider; private ClaimsPrincipal? _cachedUser; public UserContextService(AuthenticationStateProvider authProvider) { _authProvider = authProvider; } public async Task<ClaimsPrincipal> GetUserAsync() { if (_cachedUser is not null) return _cachedUser; var authState = await _authProvider.GetAuthenticationStateAsync(); _cachedUser = authState.User; return _cachedUser; } public async Task<string?> GetUserIdAsync() { var user = await GetUserAsync(); return user.FindFirst(ClaimTypes.NameIdentifier)?.Value; } public async Task<string?> GetUsernameAsync() { var user = await GetUserAsync(); return user.FindFirst(ClaimTypes.Name)?.Value; } public async Task<string?> GetRoleAsync() { var user = await GetUserAsync(); return user.FindFirst(ClaimTypes.Role)?.Value; } public async Task<string?> GetModifiedIdAsync() { var user = await GetUserAsync(); return user.FindFirst("modified_id")?.Value; } public async Task<string?> GetAuthSchemeAsync() { var user = await GetUserAsync(); return user.FindFirst("auth_scheme")?.Value; } public async Task<bool> IsInRoleAsync(string role) { var user = await GetUserAsync(); return user.IsInRole(role); } public async Task<bool> IsAuthenticatedAsync() { var user = await GetUserAsync(); return user.Identity?.IsAuthenticated ?? false; } }