Families.Web 1.0.0.0
사진, 동영상, 글, 댓글과 반응을 가족끼리만 공유하는 비공개 앨범·타임라인 서비스입니다.
로딩중...
검색중...
일치하는것 없음
FamilyUserContext.cs
이 파일의 문서화 페이지로 가기
1using System.Security.Claims;
2using Dreamine.Identity;
3using Microsoft.AspNetCore.Components.Authorization;
4
6
15public sealed record FamilyCurrentUser(
16 string Id,
17 string Provider,
18 string Email,
19 string DisplayName)
20{
29 public bool IsAuthenticated => !string.IsNullOrWhiteSpace(Id);
30
39 public static FamilyCurrentUser Anonymous { get; } = new("", "", "", "");
40}
41
50public sealed class FamilyUserContext
51{
60 private readonly AuthenticationStateProvider _authenticationStateProvider;
61
78 public FamilyUserContext(AuthenticationStateProvider authenticationStateProvider)
79 {
80 _authenticationStateProvider = authenticationStateProvider;
81 }
82
99 public async Task<FamilyCurrentUser> GetCurrentAsync()
100 {
101 var state = await _authenticationStateProvider.GetAuthenticationStateAsync().ConfigureAwait(false);
102 var principal = state.User;
103
104 if (principal.Identity?.IsAuthenticated != true)
105 {
106 return FamilyCurrentUser.Anonymous;
107 }
108
109 string userId = principal.FindFirstValue(DreamineIdentityExtensions.UserIdClaimType) ?? string.Empty;
110 if (string.IsNullOrWhiteSpace(userId))
111 {
112 return FamilyCurrentUser.Anonymous;
113 }
114
115 string provider = principal.FindFirstValue(DreamineIdentityExtensions.ProviderClaimType) ?? "Local";
116 string email = principal.FindFirstValue(ClaimTypes.Email) ?? string.Empty;
117 string displayName = principal.FindFirstValue(ClaimTypes.Name) ?? email;
118
119 return new FamilyCurrentUser(
120 $"oauth-{userId}",
121 provider,
122 email,
123 displayName);
124 }
125}
record FamilyCurrentUser(string Id, string Provider, string Email, string DisplayName)
FamilyUserContext(AuthenticationStateProvider authenticationStateProvider)
async Task< FamilyCurrentUser > GetCurrentAsync()
readonly AuthenticationStateProvider _authenticationStateProvider