11.07.2015 Views

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

136 Part III: Building Secure <strong>Web</strong> <strong>Application</strong>sThe following example shows imperative security syntax using aPrincipalPermission object.PrincipalPermission permCheck = new PrincipalPermission(null, @"DomainName\WindowsGroup");permCheck.Dem<strong>and</strong>();To avoid a local variable, the code above can also be written as:(new PrincipalPermission(null, @"DomainName\WindowsGroup")).Dem<strong>and</strong>();The code creates a PrincipalPermission object with a blank user name <strong>and</strong> a specifiedrole name, <strong>and</strong> then calls the Dem<strong>and</strong> method. This causes the common languageruntime to interrogate the current Principal object that is attached to the currentthread <strong>and</strong> check whether the associated identity is a member of the specified role.Because Windows authentication is used in this example, the role check uses aWindows group. If the current identity is not a member of the specified role, a<strong>Security</strong>Exception is thrown.The following example shows how to restrict access to an individual user.(new PrincipalPermission(@"DOMAINNAME\James", null)).Dem<strong>and</strong>();Declarative vs. Imperative <strong>Security</strong>You can use role-based security (<strong>and</strong> code access security) either declaratively usingattributes or imperatively in code. Generally, declarative security offers the mostbenefits, although sometimes you must use imperative security (for example, whenyou need to use variables that are only available at runtime) to help make a securitydecision.Advantages of Declarative <strong>Security</strong>The main advantages of declarative security are the following:● It allows the administrator or assembly consumer to see precisely which securitypermissions that particular classes <strong>and</strong> methods must run. Tools such aspermview.exe provide this information. Knowing this information at deploymenttime can help resolve security issues <strong>and</strong> it helps the administrator configure codeaccess security policy.● It offers increased performance. Declarative dem<strong>and</strong>s are evaluated only once atload time. Imperative dem<strong>and</strong>s inside methods are evaluated each time themethod that contains the dem<strong>and</strong> is called.

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

Saved successfully!

Ooh no, something went wrong!