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.

552 ❘ ChaPTer 21 security<br />

<br />

<br />

<br />

<br />

<br />

The Windows application just uses Label, TextBox, PasswordBox,<br />

<strong>and</strong> Button controls, as shown in Figure 21-1. The Label with<br />

the content User Validated shows up only when the logon is<br />

successful.<br />

The h<strong>and</strong>ler of the Button.Click event invokes the ValidateUser()<br />

method of the Membership class. Because of the configured provider<br />

ClientAuthenticationMembershipProvider, the provider in turn<br />

invokes the Web service <strong>and</strong> calls the method ValidateUser() of the<br />

SampleMembershipProvider class to verify a successful logon. With<br />

success, the Label labelValidatedInfo is made visible; otherwise, a<br />

message box pops up:<br />

private void OnLogin(object sender, RoutedEventArgs e)<br />

{<br />

try<br />

figure 21-1<br />

{<br />

if (Membership.ValidateUser(textUsername.Text,<br />

textPassword.Password))<br />

{<br />

// user validated!<br />

labelValidatedInfo.Visibility = Visibility.Visible;<br />

}<br />

else<br />

{<br />

MessageBox.Show("Username or password not valid",<br />

"Client Authentication Services", MessageBoxButton.OK,<br />

MessageBoxImage.Warning);<br />

}<br />

}<br />

catch (WebException ex)<br />

{<br />

MessageBox.Show(ex.Message, "Client Application Services",<br />

MessageBoxButton.OK, MessageBoxImage.Error);<br />

}<br />

}<br />

code snippet AuthenticationServices/app.config<br />

code snippet AuthenticationServices/MainWindow.xaml.cs<br />

enCryPTion<br />

Confidential data should be secured so that it cannot be read by unprivileged users. This is valid both<br />

for data that is sent across the network, or stored data. You can encrypt such data with symmetric or<br />

asymmetric encryption keys.<br />

With a symmetric key, the same key can be used for encryption <strong>and</strong> decryption. With asymmetric<br />

encryption, different keys are used for encryption <strong>and</strong> decryption: a public <strong>and</strong> a private key. Something<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!