using System.Security.Principal; string username = "John"; GenericIdentity identity = new GenericIdentity(username); string[] roles = {"user", "admin"}; GenericPrincipal principal = new GenericPrincipal(identity, roles); // Check if user is authenticated and authorized if (principal.Identity.IsAuthenticated && principal.IsInRole("admin")) { Console.WriteLine("User is authenticated and authorized."); }
using System.Security.Principal; string username = "John"; GenericIdentity identity = new GenericIdentity(username); // Set custom authentication type identity = new GenericIdentity(username, "CustomAuthenticationType"); // Get or set name associated with the identity Console.WriteLine(identity.Name); // John identity.Name = "Jane"; Console.WriteLine(identity.Name); // JaneThe System.Security.Principal namespace is part of the .NET Framework Class Library.