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

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

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

Chapter 10: Building Secure ASP.NET Pages <strong>and</strong> Controls 285Use Explicit Role Checks for Fine-Grained AuthorizationDeclarative security checks prevent a user from accessing a class or calling a specificmethod. If you need additional logic inside a method to make authorizationdecisions, either use imperative principal permission dem<strong>and</strong>s or explicit role checksusing IPrincipal.IsInRole. These approaches allow you to use additional runtimevariables to fine tune the authorization decision. The following example shows theuse of an imperative principal permission dem<strong>and</strong>:// Imperative syntaxpublic void SomeRestrictedMethod(){// Only callers that are members of the specified Windows group// are allowed accessPrincipalPermission permCheck = new PrincipalPermission(null, @"DomainName\WindowsGroup");permCheck.Dem<strong>and</strong>();// Some restricted operations (omitted)}The following example shows the use of IPrincipal.IsInRole:public void TransferMoney( string fromAccount,string toAccount, double amount){// Extract the authenticated user from the current HTTP context.// The User variable is equivalent to HttpContext.Current.User if you// are using an .aspx page (or .asmx)WindowsPrincipal authenticatedUser = User as WindowsPrincipal;if (null != authenticatedUser){// Note: To retrieve the authenticated user's username, use the// following line of code// string username = authenticatedUser.Identity.Name;// If the amount exceeds a threshold value, manager approval is requiredif (amount > thresholdValue) {// Perform a role checkif (authenticatedUser.IsInRole(@"DomainName\Manager") ){// OK to proceed with transfer}else{throw new Exception("Unauthorized funds transfer");}}else{. . .}}}

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

Saved successfully!

Ooh no, something went wrong!