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.

authentication <strong>and</strong> authorization ❘ 547<br />

The output from this console application looks similar to the following lines; it will vary according to your<br />

machine’s configuration <strong>and</strong> the roles associated with the account under which you are signed in:<br />

IdentityType:System.Security.Principal.WindowsIdentity<br />

Name: farabove\christian<br />

'Users' True<br />

'Administrators' False<br />

Authenticated: True<br />

AuthType: NTLM<br />

Anonymous False<br />

Token: 416<br />

It is enormously beneficial to be able to easily access details about the current users <strong>and</strong> their roles. With<br />

this information, you can make decisions about what actions should be permitted or denied. The ability<br />

to make use of roles <strong>and</strong> Windows user groups provides the added benefit that administration can be done<br />

by using st<strong>and</strong>ard user administration tools, <strong>and</strong> you can usually avoid altering the code when user roles<br />

change. The following section looks at roles in more detail.<br />

roles<br />

Role-based security is especially useful in situations in which access to resources is an issue. A primary<br />

example is the finance industry, in which employees’ roles define what information they can access <strong>and</strong> what<br />

actions they can perform.<br />

Role-based security is also ideal for use in conjunction with Windows accounts, or a custom user directory<br />

to manage access to web-based resources. For example, a web site could restrict access to its content until<br />

a user registers with the site, <strong>and</strong> then additionally provide access to special content only, if the user is a<br />

paying subscriber. In many ways, ASP.<strong>NET</strong> makes role-based security easier because much of the code is<br />

based on the server.<br />

For example, to implement a Web service that requires authentication, you could use the account subsystem<br />

of Windows <strong>and</strong> write the web method in such a way that it ensures the user is a member of a specific<br />

Windows user group before allowing access to the method’s functionality.<br />

Imagine a scenario with an intranet application that relies on Windows accounts. The system has a group<br />

called Manager <strong>and</strong> one called Assistant; users are assigned to these groups according to their role within<br />

the organization. Say that the application contains a feature that displays information about employees that<br />

should be accessed only by users in the Managers group. You can easily use code that checks whether the<br />

current user is a member of the Managers group <strong>and</strong> whether he is permitted or denied access.<br />

However, if you decide later to rearrange the account groups <strong>and</strong> to introduce a group called Personnel<br />

that also has access to employee details, you will have a problem. You will need to go through all the code<br />

<strong>and</strong> update it to include rules for this new group.<br />

A better solution would be to create a permission called something like ReadEmployeeDetails <strong>and</strong> assign it<br />

to groups where necessary. If the code applies a check for the ReadEmployeeDetails permission, updating<br />

the application to allow those in the Personnel group access to employee details is simply a matter of<br />

creating the group, placing the users in it, <strong>and</strong> assigning the ReadEmployeeDetails permission.<br />

declarative role-based security<br />

Just as with code access security, you can implement role-based security requests (“the user must be in the<br />

Administrators group”) using imperative requests by calling the IsInRole() method from the IPrincipal<br />

class, or using attributes. You can state permission requirements declaratively at the class or method level<br />

using the [PrincipalPermission] attribute:<br />

using System;<br />

using System.Security;<br />

using System.Security.Principal;<br />

using System.Security.Permissions;<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!