BotSharp/src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs

32 lines
952 B
C#
Raw Normal View History

2023-06-17 02:42:35 +00:00
using Microsoft.AspNetCore.Http;
using System.Security.Claims;
namespace BotSharp.Core.Users.Services;
public class UserIdentity : IUserIdentity
{
private readonly IHttpContextAccessor _contextAccessor;
2023-11-10 16:01:03 +00:00
private IEnumerable<Claim> _claims => _contextAccessor.HttpContext?.User.Claims!;
2023-06-17 02:42:35 +00:00
public UserIdentity(IHttpContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;
}
public string Id
2023-11-10 16:01:03 +00:00
=> _claims?.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value!;
2023-06-17 02:42:35 +00:00
2024-01-05 03:20:50 +00:00
public string UserName
=> _claims?.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value!;
public string Email
2023-11-10 16:01:03 +00:00
=> _claims?.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value!;
2023-06-17 02:42:35 +00:00
public string FirstName
2023-11-10 16:01:03 +00:00
=> _claims?.FirstOrDefault(x => x.Type == ClaimTypes.GivenName)?.Value!;
2023-06-17 02:42:35 +00:00
public string LastName
2023-11-10 16:01:03 +00:00
=> _claims?.FirstOrDefault(x => x.Type == ClaimTypes.Surname)?.Value!;
2023-06-17 02:42:35 +00:00
}