The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
GenericPrincipal Class
.NET Framework 2.0
Represents a generic principal.
Namespace: System.Security.Principal
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
[SerializableAttribute] [ComVisibleAttribute(true)] public class GenericPrincipal : IPrincipal
/** @attribute SerializableAttribute() */ /** @attribute ComVisibleAttribute(true) */ public class GenericPrincipal implements IPrincipal
SerializableAttribute ComVisibleAttribute(true) public class GenericPrincipal implements IPrincipal
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; } }
import System.*;
import System.Security.Principal.*;
class GenericPrincipalMembers
{
/** @attribute STAThread()
*/
public 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.get_Identity());
// Display the identity name and authentication type.
if (principalIdentity.get_IsAuthenticated()) {
Console.WriteLine(principalIdentity.get_Name());
Console.WriteLine("Type:" + principalIdentity.
get_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();
} //main
// 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.get_IsAuthenticated()) {
// Add custom NetworkUser role.
roles.set_Item(0, "NetworkUser");
}
if (windowsIdentity.get_IsGuest()) {
// Add custom GuestUser role.
roles.set_Item(1, "GuestUser");
}
if (windowsIdentity.get_IsSystem()) {
// Add custom SystemUser role.
roles.set_Item(2, "SystemUser");
}
// Construct a GenericIdentity object based on the current Windows
// identity name and authentication type.
String authenticationType = windowsIdentity.get_AuthenticationType();
String userName = windowsIdentity.get_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;
} //GetGenericPrincipal
} //GenericPrincipalMembers
Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Community Additions
Show: