03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

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

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

The Role Manager x 635<br />

Management section appear aga<strong>in</strong>. Next, log out aga<strong>in</strong>, log <strong>in</strong> with one of the other accounts, <strong>and</strong><br />

then try to access the Management folder aga<strong>in</strong>. Because that account doesn’t have permissions to<br />

access that folder, you’re redirected to the Log<strong>in</strong> page aga<strong>in</strong>.<br />

How It Works<br />

To see how this works, you need to look at a couple of th<strong>in</strong>gs. First, look at the sett<strong>in</strong>gs you added to<br />

the Web.config file to limit access to the Management folder:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

When the <strong>ASP</strong>.<strong>NET</strong> run time processes the request for a page, it checks the various configuration<br />

files to see whether the current user is allowed to access that resource. For requests to files <strong>in</strong> the<br />

Management folder, it encounters the rule set <strong>in</strong> the element. It starts scann<strong>in</strong>g the various<br />

allow <strong>and</strong> deny elements with roles or users attributes to specify the users or roles that are<br />

affected by the rule. The roles <strong>and</strong> users attributes take one or more role or user names, separated<br />

by a comma. As soon as a rule is found that matches, the scann<strong>in</strong>g process is stopped <strong>and</strong> that rule is<br />

applied. If no rule is satisfied, access is granted! Therefore, it’s important to end the rule with a deny<br />

rule to block all other users that haven’t been granted access previously.<br />

If you’d add an authorization element to the Web.config <strong>in</strong> the Management folder, the sett<strong>in</strong>gs you<br />

apply there are looked at first because the security model works <strong>in</strong>side out. That is, it starts by scann<strong>in</strong>g<br />

the Web.config file (if present) <strong>in</strong> the folder that conta<strong>in</strong>s the requested page. If it doesn’t f<strong>in</strong>d the file<br />

there or it doesn’t conta<strong>in</strong> sett<strong>in</strong>gs that block or grant access, it goes up <strong>in</strong> the folder hierarchy search<strong>in</strong>g<br />

for configuration files with authorization elements. In the previous exercise, the run-time found the<br />

sett<strong>in</strong>gs <strong>in</strong> the Web.config file <strong>in</strong> the root that were then applied to the folder.<br />

When an unauthenticated user logs <strong>in</strong>, the first rule won’t match because the anonymous user is not a<br />

member of the Managers role. The user is then denied access because of the deny rule that blocks all<br />

users, <strong>in</strong>dicated by the asterisk (*).<br />

After you logged <strong>in</strong> as a Manager <strong>and</strong> requested the same resource, the rule set was scanned aga<strong>in</strong>. The<br />

run time then found the allow element that grants access to the Managers role <strong>and</strong> immediately let<br />

you <strong>in</strong>. The f<strong>in</strong>al rule that blocks access to all other users was not even checked. In addition to specific<br />

roles or usernames <strong>and</strong> the asterisk (*) to refer to all users, you can also use the question mark (?) to<br />

refer to unauthenticated — or anonymous — users. So, for example, to let any logged-<strong>in</strong> user access the<br />

Reviews folder regardless of the role they are <strong>in</strong>, <strong>and</strong> block access to all other users, you can add the<br />

follow<strong>in</strong>g element to your configuration file:<br />

<br />

<br />

<br />

<br />

<br />

<br />

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

Saved successfully!

Ooh no, something went wrong!