65 lines
1.4 KiB
C#
65 lines
1.4 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Security.Cryptography;
|
|||
|
namespace ln.application
|
|||
|
{
|
|||
|
public class AuthenticatedUser
|
|||
|
{
|
|||
|
public static AuthenticatedUser Anonymous { get; } = new AuthenticatedUser();
|
|||
|
|
|||
|
public Guid ID { get; }
|
|||
|
public string Name { get; }
|
|||
|
|
|||
|
public byte[] SecretHash { get; set; }
|
|||
|
|
|||
|
public int Token { get; set; }
|
|||
|
|
|||
|
public string[] Roles => Roles.ToArray();
|
|||
|
|
|||
|
|
|||
|
HashSet<string> roles = new HashSet<string>();
|
|||
|
|
|||
|
public AuthenticatedUser()
|
|||
|
{
|
|||
|
ID = Guid.Empty;
|
|||
|
Name = "Anonymous";
|
|||
|
}
|
|||
|
|
|||
|
public AuthenticatedUser(Guid userID, string name) : this(userID, name, new string[0]) { }
|
|||
|
public AuthenticatedUser(Guid userID, string name, IEnumerable<string> userRoles)
|
|||
|
{
|
|||
|
ID = userID;
|
|||
|
Name = name;
|
|||
|
foreach (string role in userRoles)
|
|||
|
roles.Add(role);
|
|||
|
}
|
|||
|
|
|||
|
public bool HasRole(string role) => roles.Contains(role);
|
|||
|
|
|||
|
public void SetSecret(string secret)
|
|||
|
{
|
|||
|
byte[] secretBytes = Encoding.UTF8.GetBytes(secret);
|
|||
|
SHA256 sha256 = SHA256.Create();
|
|||
|
sha256.ComputeHash(secretBytes);
|
|||
|
|
|||
|
SecretHash = sha256.Hash;
|
|||
|
}
|
|||
|
|
|||
|
public override int GetHashCode()
|
|||
|
{
|
|||
|
return ID.GetHashCode();
|
|||
|
}
|
|||
|
public override bool Equals(object obj)
|
|||
|
{
|
|||
|
if (obj is AuthenticatedUser)
|
|||
|
{
|
|||
|
AuthenticatedUser other = obj as AuthenticatedUser;
|
|||
|
return ID.Equals(other.ID);
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|