15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

546 ❘ ChaPTer 21 security<br />

classes that implement the interface IIdentity. With this interface you have access to the name of the user,<br />

information about whether the user is authenticated, <strong>and</strong> the authentication type.<br />

A principal is an object that contains the identity of the user <strong>and</strong> the roles that the user belongs to. The<br />

interface IPrincipal defines the property Identity, which returns an IIdentity object, <strong>and</strong> the method<br />

IsInRole with which you can verify that the user is a member of a specific role. A role is a collection of<br />

users who have the same security permissions, <strong>and</strong> it is the unit of administration for users. Roles can be<br />

Windows groups or just a collection of strings that you define.<br />

The principal classes available with .<strong>NET</strong> are WindowsPrincipal <strong>and</strong> GenericPrincipal. You can also<br />

create a custom principal class that implements the interface IPrincipal.<br />

In the following example, you create a console application that provides access to the principal in an<br />

application that, in turn, enables you to access the underlying Windows account. You need to import the<br />

System.Security.Principal <strong>and</strong> System.Threading namespaces. First, you must specify that .<strong>NET</strong><br />

should automatically hook up the principal with the underlying Windows account. This must be done<br />

because .<strong>NET</strong> does not automatically populate the thread’s CurrentPrincipal property for security<br />

reasons. You can do it like this:<br />

using System;<br />

using System.Security.Principal;<br />

using System.Threading;<br />

namespace Wrox.ProCSharp.Security<br />

{<br />

class Program<br />

{<br />

static void Main()<br />

{<br />

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);<br />

code snippet WindowsPrincipal/Program.cs<br />

It is possible to use WindowsIdentity.GetCurrent() to access the Windows account details; however,<br />

that method is best used when you are going to look at the principal only once. If you want to access the<br />

principal a number of times, it is more efficient to set the policy so that the current thread provides access<br />

to the principal for you. If you use the SetPrincipalPolicy method, it is specified that the principal in<br />

the current thread should hold a WindowsIdentity object. All identity classes, such as WindowsIdentity,<br />

implement the IIdentity interface. The interface contains three properties (AuthenticationType,<br />

IsAuthenticated, <strong>and</strong> Name) for all derived identity classes to implement.<br />

Add code to access the principal’s properties from the Thread object:<br />

}<br />

}<br />

}<br />

WindowsPrincipal principal =<br />

(WindowsPrincipal)Thread.CurrentPrincipal;<br />

WindowsIdentity identity = (WindowsIdentity)principal.Identity;<br />

Console.WriteLine("IdentityType: " + identity.ToString());<br />

Console.WriteLine("Name: {0}", identity.Name);<br />

Console.WriteLine("'Users': {0} ",<br />

principal.IsInRole(WindowsBuiltInRole.User));<br />

Console.WriteLine("'Administrators' {0}",<br />

principal.IsInRole(WindowsBuiltInRole.Administrator));<br />

Console.WriteLine("Authenticated: {0}", identity.IsAuthenticated);<br />

Console.WriteLine("AuthType: {0}", identity.AuthenticationType);<br />

Console.WriteLine("Anonymous {0}", identity.IsAnonymous);<br />

Console.WriteLine("Token: {0}", identity.Token);<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!