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 6: .NET <strong>Security</strong> Overview 135Declarative <strong>Security</strong>You can control precisely which users should be allowed to access a class or a methodby adding a PrincipalPermissionAttribute to the class or method definition. A classlevelattribute automatically applies to all class members unless it is overridden by amember-level attribute. The PrincipalPermissionAttribute type is defined within theSystem.<strong>Security</strong>.Permissions namespace.Note You can also use the PrincipalPermissionAttribute to restrict access to structures <strong>and</strong> toother member types, such as properties <strong>and</strong> delegates.The following example shows how to restrict access to a particular class to membersof a Managers group. Note that this example assumes Windows authentication,where the format of the role name is in the format MachineName\RoleName orDomainName\RoleName. For other authentication types, the format of the role name isapplication specific <strong>and</strong> depends on the role-name strings held in the user store.[PrincipalPermissionAttribute(<strong>Security</strong>Action.Dem<strong>and</strong>, Role=@"DOMAINNAME\Managers")]public sealed class OnlyManagersCanCallMe{}Note The trailing Attribute can be omitted from the attribute type names. This makes the attributetype name appear to be the same as the associated permission type name, which in this case isPrincipalPermission. They are distinct (but logically related) types.The next example shows how to restrict access to a particular method on a class. Inthis example, access is restricted to members of the local administrators group, whichis identified by the special “BUILTIN\Administrators” identifier.[PrincipalPermissionAttribute(<strong>Security</strong>Action.Dem<strong>and</strong>,Role=@"BUILTIN\Administrators")]public void SomeMethod(){}Other built-in Windows group names can be used by prefixing the group name with“BUILTIN\” (for example, “BUILTIN\Users” <strong>and</strong> “BUILTIN\Power Users”).Imperative <strong>Security</strong>If method-level security is not granular enough for your security requirements, youcan perform imperative security checks in code by usingSystem.<strong>Security</strong>.Permissions.PrincipalPermission objects.

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

Saved successfully!

Ooh no, something went wrong!