This documentation is archived and is not being maintained.
GenericPrincipal Class
Visual Studio 2008
Represents a generic principal.
Assembly: mscorlib (in mscorlib.dll)
This class represents the roles of the current user.
Topic | Location |
---|---|
How to: Create GenericPrincipal and GenericIdentity Objects | .NET Framework: Security |
How to: Create GenericPrincipal and GenericIdentity Objects | .NET Framework: Security |
The following example shows the use of members of the GenericPrincipal class.
using System; using System.Security.Principal; class GenericPrincipalMembers { [STAThread] static void Main(string[] args) { // Retrieve a GenericPrincipal that is based on the current user's // WindowsIdentity. GenericPrincipal genericPrincipal = GetGenericPrincipal(); // Retrieve the generic identity of the GenericPrincipal object. GenericIdentity principalIdentity = (GenericIdentity)genericPrincipal.Identity; // Display the identity name and authentication type. if (principalIdentity.IsAuthenticated) { Console.WriteLine(principalIdentity.Name); Console.WriteLine("Type:"+principalIdentity.AuthenticationType); } // Verify that the generic principal has been assigned the // NetworkUser role. if (genericPrincipal.IsInRole("NetworkUser")) { Console.WriteLine("User belongs to the NetworkUser role."); } Console.WriteLine("The sample completed successfully; " + "press Enter to continue."); Console.ReadLine(); } // Create a generic principal based on values from the current // WindowsIdentity. private static GenericPrincipal GetGenericPrincipal() { // Use values from the current WindowsIdentity to construct // a set of GenericPrincipal roles. WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent(); string[] roles = new string[10]; if (windowsIdentity.IsAuthenticated) { // Add custom NetworkUser role. roles[0] = "NetworkUser"; } if (windowsIdentity.IsGuest) { // Add custom GuestUser role. roles[1] = "GuestUser"; } if (windowsIdentity.IsSystem) { // Add custom SystemUser role. roles[2] = "SystemUser"; } // Construct a GenericIdentity object based on the current Windows // identity name and authentication type. string authenticationType = windowsIdentity.AuthenticationType; string userName = windowsIdentity.Name; GenericIdentity genericIdentity = new GenericIdentity(userName, authenticationType); // Construct a GenericPrincipal object based on the generic identity // and custom roles for the user. GenericPrincipal genericPrincipal = new GenericPrincipal(genericIdentity, roles); return genericPrincipal; } }
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: