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.

382 Part III: Building Secure <strong>Web</strong> <strong>Application</strong>sRestrict Unauthorized CallersYou code should authorize users based on a role or identity before it connects to thedatabase. Role checks are usually used in the business logic of your application, but ifyou do not have a clear distinction between business <strong>and</strong> data access logic, useprincipal permission dem<strong>and</strong>s on the methods that access the database.The following attribute ensures that only users who are members of the Manager rolecan call the DisplayCustomerInfo method:[PrincipalPermissionAttribute(<strong>Security</strong>Action.Dem<strong>and</strong>, Role="Manager")]public void DisplayCustomerInfo(int CustId){}If you need additional authorization granularity <strong>and</strong> need to perform role-based logicinside the data access method, use imperative principal permission dem<strong>and</strong>s orexplicit role checks as shown in the following code fragment:using System.<strong>Security</strong>;using System.<strong>Security</strong>.Permissions;public void DisplayCustomerInfo(int CustId){try{// Imperative principal permission role check to verify that the caller// is a managerPrincipalPermission principalPerm = new PrincipalPermission(null, "Manager");// Code that follows is only executed if the caller is a member// of the "Manager" role}catch( <strong>Security</strong>Exception ex ){. . .}}The following code fragment uses an explicit, programmatic role check to ensure thatthe caller is a member of the Manager role:public void DisplayCustomerInfo(int CustId){if(!Thread.CurrentPrincipal.IsInRole("Manager")){. . .}}

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

Saved successfully!

Ooh no, something went wrong!