Download presentation
Presentation is loading. Please wait.
1
System.Security.Principal Namespace
Presenter Kranthi Kumar Kontham
2
Introduction Identity: Represents an authenticated user. Principal: Encapsulates an identity and the roles to which the identity belongs.
3
Role-based Security in .net Framework supports
three types of principals: Generic Principal Independent of Windows account users and roles. Window Principal Represents Windows users and roles. Custom Principal: Extension of basic idea of principal’s identity and roles. Customised according to particular application.
4
Classes GenericIdentity GenericPrincipal SecurityIdentifier
WindowsIdentity WindowsPrincipal
5
Example GenericPrincipal
//Data is normally retrieved from the database String username=“Alex”; String[] roles=new String[] {“Programmer”, “ Teacher”}; GenericIdentity identity; GenericPrincipal principal; //create identity and principal Identity= new GenericIdentiy(username,“customauthenication”); Principal=new GenericPrincipal(identity, roles); //set principal to thread Thread.CurrentPrincipal=principal;
6
Interfaces Properties: IIdentity Iprincipal IsAuthenticated
AuthenticationType (Basic, ntlm, kerberos and passport authentication) Name Iprincipal Identity IsInRole() Basic, ntlm, kerberos and passport authenticarion
7
Enumerations PrincipalPolicy
public enum PrincipalPolicy { UnauthenticatedPrincipal = 0, NoPrincipal = 1,WindowsPrincipal = 2 } WindowsAccountType Normal = 0,Guest = 1,System = 2,Anonymous = 3 WindowsBuiltInRole Administrator = 544,User = 545,Guest = 546,PowerUser = 547,AccountOperator = 548, SystemOperator = 549, PrintOperator = 550,BackupOperator = 551,Replicator = 552
8
Code Snippet 1 AppDomain myDomain = Thread.GetDomain();
//creating appl. Domain obj and returning curr. domain in which curr. Thread is running AppDomain myDomain = Thread.GetDomain(); // Configure the current application domain's principal policy to represent the active Windows user myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal; Array wbirFields = Enum.GetValues(typeof(WindowsBuiltInRole)); foreach (object roleName in wbirFields) { try // Cast the role name to a RID represented by the WindowsBuildInRole value. Console.WriteLine("{0}? {1}.", roleName, myPrincipal.IsInRole((WindowsBuiltInRole)roleName)); }
9
Demo
10
Code snippet 2 // Create a WindowsIdentity object for the user represented by the specified Windows account token. private static void IntPtrConstructor(IntPtr logonToken) { // Construct a WindowsIdentity object using the input account token. WindowsIdentity windowsIdentity = new WindowsIdentity(logonToken); Console.WriteLine("Created a Windows identity object named " + windowsIdentity.Name + "."); } // Construct a WindowsIdentity object using the input account token and the specified authentication type. string authenticationType = "WindowsAuthentication1"; WindowsIdentity windowsIdentity = new WindowsIdentity(logonToken, authenticationType);
11
string authenticationType = "WindowsAuthentication";
// Construct a WindowsIdentity object using the input account token, // and the specified authentication type, and Windows account type. string authenticationType = "WindowsAuthentication"; WindowsAccountType guestAccount = WindowsAccountType.Guest; WindowsIdentity windowsIdentity = new WindowsIdentity(logonToken, authenticationType, guestAccount); // and the specified authentication type, Windows account type, and // authentication flag. bool isAuthenticated = true; WindowsIdentity windowsIdentity = new WindowsIdentity( logonToken, authenticationType, guestAccount, isAuthenticated);
12
private static void UseProperties(IntPtr logonToken) {
// Access the properties of a WindowsIdentity object. private static void UseProperties(IntPtr logonToken) { WindowsIdentity windowsIdentity = new WindowsIdentity(logonToken); propertyDescription += windowsIdentity.Name; if (!windowsIdentity.IsAnonymous) { } if (windowsIdentity.IsAuthenticated) if (windowsIdentity.IsSystem) }
13
if (windowsIdentity.IsGuest) { }
{ } IntPtr accountToken = WindowsIdentity.GetCurrent().Token; // Retrieve a WindowsIdentity object that represents an anonymous // Windows user WindowsIdentity windowsIdentity = WindowsIdentity.GetAnonymous();
14
Demo
15
Questions ?
16
References www.msdn.com
Professional C#, Third Edition, Copyright © 2004 by Wiley Publishing, Inc., Indianapolis, Indiana. All rights reserved.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.