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.

Chapter 6: .NET <strong>Security</strong> Overview 137●●<strong>Security</strong> attributes ensure that the permission dem<strong>and</strong> is executed before anyother code in the method has a chance to run. This eliminates potential bugswhere security checks are performed too late.Declarative checks at the class level apply to all class members. Imperative checksapply at the call site.Advantages of Imperative <strong>Security</strong>The main advantages of imperative security <strong>and</strong> the main reasons that yousometimes must use it are:● It allows you to dynamically shape the dem<strong>and</strong> by using values only available atruntime.● It allows you to perform more granular authorization by implementingconditional logic in code.Role-Based <strong>Security</strong> ChecksFor fine-grained authorization decisions, you can also perform explicit role checks byusing the IPrincipal.IsInRole method. The following example assumes Windowsauthentication, although the code would be very similar for Forms authentication,except that you would cast the User object to an object of the GenericPrincipal type.// Extract the authenticated user from the current HTTP context.// The User variable is equivalent to HttpContext.Current.User if you are using// an .aspx or .asmx pageWindowsPrincipal authenticatedUser = User as WindowsPrincipal;if (null != authenticatedUser){// Note: If you need to authorize specific users based on their identity// <strong>and</strong> not their role membership, you can retrieve the authenticated user's// username with the following line of code (normally though, you should// perform role-based authorization).// string username = authenticatedUser.Identity.Name;// Perform a role checkif (authenticatedUser.IsInRole(@"DomainName\Manager") ){// User is authorized to perform manager functionality}}else{// User is not authorized to perform manager functionality// Throw a security exception}

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

Saved successfully!

Ooh no, something went wrong!