Providing Username and Password Information from within an Application

In the previous section, you learned how to assign basic authentication to a web service that requires the user to provide a valid username and password. To connect to the web service, you used your browser, which knows how to respond to a server’s request for username and password information. The Visual Basic .NET program in Listing 8.2, NoAccess.vb, tries to access the ShowUser web service. Unfortunately, when the server requests authentication information from the program, the program does not know how to respond. As a result, the program cannot access the web service and will generate the exception shown in Figure 8.7.

click to expand
Figure 8.7: If a program cannot respond to a server’s request for authentication, the server will deny the program’s access to a web service.

To create the NoAccess.vb program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click Windows Application. Finally, within the Location field, specify the folder within which you want to store the program, and within the name field, type the program name NoAccess. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop the button and text box previously shown in Figure 8.7 onto the page.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type localhost/ShowUser/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 8.2.

Listing 8.2 NoAccess.vb

start example
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _    System.EventArgs) Handles Button1.Click   Try     Dim WS As New localhost.Service1()     Button1.Enabled = False     TextBox1.Text = WS.Username()     Button1.Enabled = True   Catch Ex As Exception     TextBox1.Text = Ex.Message   End Try End Sub
end example

For a program to access a web service that requires basic authentication, the program must provide the server with username and password information. The program in Listing 8.3, ProvideUserinfo.vb, displays the form, shown in Figure 8.8, that you can use to specify the username and password the remote server can use to authenticate you.

click to expand
Figure 8.8: Prompting the user for login information the program can send to a remote server

After you enter the username and password information, the program creates a NetworkCredentials object, to which it assigns the data. The program then assigns the credentials to the web service object. When the program later requests a connection to a web service method, the program can provide the credentials that the server can use to authenticate each request.

To create the ProvideUserinfo.vb program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click Windows Application. Finally, within the Location field, specify the folder within which you want to store the program and within the name field, type the program name ProvideUserinfo. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop the button and text box previously shown in Figure 8.8 onto the page.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type localhost/ShowUser/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 8.3.

Listing 8.3 ProvideUserinfo.vb

start example
Imports System.Net Private Sub Button1_Click(ByVal sender As System.Object, _     ByVal e As  System.EventArgs) Handles Button1.Click   Try     If (TextBox1.Text.Length > 0) Then       If (TextBox2.Text.Length > 0) Then         Dim WS As New localhost.Service1()         Button1.Enabled = False         Dim Userinfo As New NetworkCredential(TextBox1.Text, _           TextBox2.Text)         WS.Credentials = Userinfo         TextBox3.Text = "Username: " & WS.Username()         Button1.Enabled = True       Else         TextBox3.Text = "Must specify password"       End If     Else       TextBox3.Text = "Must Specify Username"     End If   Catch Ex As Exception     TextBox3.Text = Ex.Message   End Try End Sub
end example




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net