Ajoute une page utilisateur et une persistance MySQL
This commit is contained in:
40
ChessCubing.Server/Users/AuthenticatedSiteUser.cs
Normal file
40
ChessCubing.Server/Users/AuthenticatedSiteUser.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace ChessCubing.Server.Users;
|
||||
|
||||
public sealed record AuthenticatedSiteUser(
|
||||
string Subject,
|
||||
string Username,
|
||||
string? Email,
|
||||
string DisplayName);
|
||||
|
||||
public static class AuthenticatedSiteUserFactory
|
||||
{
|
||||
public static AuthenticatedSiteUser? FromClaimsPrincipal(ClaimsPrincipal user)
|
||||
{
|
||||
if (user.Identity?.IsAuthenticated != true)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var subject = user.FindFirst("sub")?.Value
|
||||
?? user.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
if (string.IsNullOrWhiteSpace(subject))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var username = user.FindFirst("preferred_username")?.Value
|
||||
?? user.Identity?.Name
|
||||
?? subject;
|
||||
var email = user.FindFirst("email")?.Value;
|
||||
var displayName = user.FindFirst("name")?.Value
|
||||
?? username;
|
||||
|
||||
return new AuthenticatedSiteUser(
|
||||
subject.Trim(),
|
||||
username.Trim(),
|
||||
string.IsNullOrWhiteSpace(email) ? null : email.Trim(),
|
||||
displayName.Trim());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user