30.5. Login Form

Earlier, when you were introduced to Forms authentication, we used a hard-coded username and password in order to validate the user. While it would be possible for the application to prompt the user for credentials before calling ValidateUser with the supplied values, there is a better way that uses the client application services framework. Instead of calling ValidateUser with a username/password combination, we go back to supplying Nothing as the argument values and define a credential provider: then the client application services will call the provider to determine the set of credentials to use.

Private Sub Form1_Load(ByVal sender As System.Object, _
                        ByVal e As System.EventArgs) Handles MyBase.Load
    If Membership.ValidateUser(Nothing, Nothing) Then
        MessageBox.Show ("User is valid")

This probably sounds more complex than it is, so let's start by adding a login form to the client application. Do this by selecting the Login Form template from the Add New Item dialog and calling it LoginForm. While you have the form designer open, click the "OK" button and change the DialogResult property to OK.

In order to use this login form as a credential provider, we will modify it to implement the IclientFormsAuthenticationCredentialsProvider interface. An alternative strategy would be to have a separate class that implements this interface and then displays the login form when the GetCredentials method is called. The following code snippet contains the code-behind file for ...

Get Professional Visual Studio® 2008 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.