|
|
When you provide users with a password or license key, one or more users will forget the information. To simplify the process of managing license keys for web services, you can create an ASP.NET page with which users can interact to request a forgotten key.
The ProvideKey ASP.NET page in Listing 15.10, for example, prompts programmers to enter the e-mail address she used to register for the service, as shown in Figure 15.5.
Figure 15.5: Automating the process of providing programmers with access to lost or forgotten keys
Behind the scenes, the page calls the GetKey web service to determine the programmer’s forgotten key. The ASP.NET page, in turn, then e-mails the key to the programmer’s e-mail address. To create the ProvideKey ASP.NET page, perform these steps:
Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.
Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name ProvideKey. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.
Using your mouse, drag and drop the text fields and buttons previously shown in Figure 15.5 onto the page.
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 15.10.
Listing 15.10 ProvideKey.aspx.vb
Imports System.Web.Mail Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents Label2 As System.Web.UI.WebControls.Label Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox Protected WithEvents Button1 As System.Web.UI.WebControls.Button #Region " Web Form Designer Generated Code " ' Code not shown. #End Region Private Sub Button1_Click(ByVal sender As System.Object, _ ÄByVal e As System.EventArgs) Handles Button1.Click Dim Key As String Try If (TextBox1.Text.Length > 0) Then Dim WS As New localhost.Service1() Key = WS.GetAddress(TextBox1.Text) Dim MailMsg As New MailMessage() MailMsg.To = TextBox1.Text MailMsg.From = "WebServiceKey" MailMsg.Subject = "Your license key" MailMsg.Body = Key MailMsg.BodyFormat = MailFormat.Text SmtpMail.Send(MailMsg) TextBox2.Text = "Mail message sent" End If Catch Ex As ExecutionEngineException TextBox2.Text = Ex.Message End Try End Sub End Class
As you can see, the code simply prompts the user to enter an e-mail address. Then, the code calls the GetAddress method to search for the specified e-mail address. If the web service returns a key, the code then e-mails that key to the specified e-mail address.
After you type the code, you must add a reference to the remote web service, by performing the following steps. (If you have not already created the GetKey web service discussed in the next section, do so now.)
Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.
Within the Address field, type http://localhost/GetKey/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.
To create the GetKey web service, perform these steps:
Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.
Within the New Project dialog box Project Types list, click Visual Basic .NET Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name GetKey. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.
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 15.11.
Listing 15.11 GetKey.asmx
Public Function LookupKey(ByVal Email As String, ByVal _ DataFile As String) As String Try Dim DataSetObj As New DataSet() DataSetObj.ReadXml(DataFile) Dim Result As String = "" Dim I As Integer For I = 0 To DataSetObj.Tables(0).Rows.Count - 1 If (Email = DataSetObj.Tables(0).Rows(I).Item("Email")) Then Result = DataSetObj.Tables(0).Rows(I).Item("Key") End If Next LookupKey = Result Catch Ex As Exception Throw Ex End Try End Function <WebMethod()> Public Function GetAddress(ByVal Email As String) _ Ä As String Dim Result As String = "" Try Result = LookupKey(Email, _ "C:\Inetpub\wwwroot\GetKey\bin\PaidUsers.xml") If (Result = "") Then Result = LookupKey(Email, _ "C:\Inetpub\wwwroot\GetKey\bin\TrialUsers.xml") If (Result = "") Then Throw New Exception("Address not found") End If End If Catch Ex As Exception Throw Ex End Try GetAddress = Result End Function
Within the GetAddress method, the code first examines the PaidUsers.xml file for the e-mail address specified. If the address is not found, the code then examines the TrialUsers.xml file. For simplicity, the LookupKey function compares the address specified to those in the XML files without regard for case (meaning that the function does not consider uppercase and lowercase letters as equivalent). A better solution would perform a case-insensitive comparison.
|
|