-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathCurrentUserService.cs
More file actions
27 lines (21 loc) · 937 Bytes
/
Copy pathCurrentUserService.cs
File metadata and controls
27 lines (21 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using Microsoft.AspNetCore.Components.Authorization;
using System.Threading.Tasks;
namespace LinkDotNet.Blog.Web.Features.Services;
public class CurrentUserService : ICurrentUserService
{
private readonly AuthenticationStateProvider authenticationStateProvider;
public CurrentUserService(AuthenticationStateProvider authenticationStateProvider)
=> this.authenticationStateProvider = authenticationStateProvider;
public async ValueTask<string?> GetDisplayNameAsync()
{
var user = (await authenticationStateProvider.GetAuthenticationStateAsync()).User;
if (user?.Identity is not { IsAuthenticated: true })
{
return null;
}
var name = user.FindFirst("Name")?.Value
?? user.FindFirst("preferred_username")?.Value
?? user.FindFirst("nickname")?.Value;
return string.IsNullOrWhiteSpace(name) ? null : name;
}
}