15.02.2015 Views

C# 4 and .NET 4

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

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

548 ❘ ChaPTer 21 security<br />

namespace Wrox.ProCSharp.Security<br />

{<br />

class Program<br />

{<br />

static void Main()<br />

{<br />

AppDomain.CurrentDomain.SetPrincipalPolicy(<br />

PrincipalPolicy.WindowsPrincipal);<br />

try<br />

{<br />

ShowMessage();<br />

}<br />

catch (SecurityException exception)<br />

{<br />

Console.WriteLine("Security exception caught ({0})", exception.Message);<br />

Console.WriteLine("The current principal must be in the local"<br />

+ "Users group");<br />

}<br />

}<br />

}<br />

}<br />

[PrincipalPermission(SecurityAction.Dem<strong>and</strong>, Role = "BUILTIN\\Users")]<br />

static void ShowMessage()<br />

{<br />

Console.WriteLine("The current principal is logged in locally ");<br />

Console.WriteLine("(member of the local Users group)");<br />

}<br />

code snippet RoleBasedSecurity/Program.cs<br />

The ShowMessage() method will throw an exception unless you execute the application in the context of a<br />

user in the Windows local Users group. For a web application, the account under which the ASP.<strong>NET</strong> code<br />

is running must be in the group, although in a “real-world” example you would certainly avoid adding this<br />

account to the administrators group!<br />

If you run the preceding code using an account in the local Users group, the output will look like this:<br />

The current principal is logged in locally<br />

(member of the local Users group)<br />

Client application services<br />

Visual Studio makes it easy to use authentication services that previously have been built for ASP.<strong>NET</strong><br />

web applications. With this service, it is possible to use the same authentication mechanism with both<br />

Windows <strong>and</strong> web applications. This is a provider model that is primarily based on the classes Membership<br />

<strong>and</strong> Roles in the namespace System.Web.Security. With the Membership class you can validate, create,<br />

delete, <strong>and</strong> find users; change the password; <strong>and</strong> do other things related to users. With the Roles class you<br />

can add <strong>and</strong> delete roles, get the roles for a user, <strong>and</strong> change roles for a user. Where the roles <strong>and</strong> users<br />

are stored depends on the provider. The ActiveDirectoryMembershipProvider accesses users <strong>and</strong> roles<br />

in the Active Directory; the SqlMembershipProvider uses a SQL Server database. With .<strong>NET</strong> 4 these<br />

providers exist for client application services ClientFormsAuthenticationMembershipProvider <strong>and</strong><br />

ClientWindowsAuthenticationMembershipProvider.<br />

Next, you use client application services with Forms authentication. To do this, first you need to start<br />

an application server, <strong>and</strong> then you can use this service from Windows Forms or Windows Presentation<br />

Foundation (WPF).<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!