Danh mục

Create a Simple XML Web Service Using Parameters

Số trang: 5      Loại file: pdf      Dung lượng: 14.63 KB      Lượt xem: 7      Lượt tải: 0    
Hoai.2512

Hỗ trợ phí lưu trữ khi tải xuống: miễn phí Tải xuống file đầy đủ (5 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Này được instructive nhưng không phải là rất hữu ích. Làm thế nào để tạo ra một dịch vụ Web có sử dụng các thông số? Đối với kỹ thuật này như thế nào-To, bạn sẽ tạo ra sự khởi đầu của một Web Dịch vụ an ninh
Nội dung trích xuất từ tài liệu:
Create a Simple XML Web Service Using Parameters 13.2 Create a Simple XML Web Service Using ParametersI have seen how to create a Web Service using the sample that Microsoft provides. Thiswas instructive but not very useful. How do I create a Web Service that uses parameters?TechniqueFor this How-To, you are going to create the start of a security Web Service. Thissecurity Web Service is going to take in two parameters: Login Name and Password. Itwill then check against a table that you will create of names, passwords, and securitylevels.The method you will create first will then pass back True or False if the name andpassword are found.Looking at the Security TableThe security table is included in the Web Services Web folder. It is calledWebServiceSecurity.MDB and is, in fact, a jet database. You can see the table created,tblUsers, in Figure 13.8. Figure 13.8. Using the tblUsers, you can look up usernames and passwordsThe method created for this first real example will take in the username and passwordand then look up the username. If the username is found, the method will then comparethe password. If the password matches, then True will be returned from the method.Otherwise, False will be returned.Passing ParametersYou will pass parameters to Web Service methods just as you would to any othermethods or functions, as shown in the function header for the Web Service methodcreated in this How-To:Public Function TestUser(ByVal strUserID As String, ByVal strPassword As String) As BooleanThe return value is also assigned to the name of the function.Specifying Descriptions for the Web Service and MethodsYou can help developers who use your Web Service by adding descriptions to the WebService and each of the methods you create. This cuts down on the number of supportrequests you get, and is a good habit to get into. For the Web Service, you will place it inthe WebService header, where you will want to specify your own namespace as well: _ Public Class SecurityServices_This causes the description specified to be displayed as the first line on the main testpage.For the method, you will include the description in the Web Method header: Public Function TestUserPassword(ByVal strUserID As String, _ ByVal strPassWord As String) As BooleanYou can see what the lines of code look like in the designer in Figure 13.9. Figure 13.9. Adding descriptions to the Web Service.Now its time to put it all together.StepsOpen and run the SecurityWebService solution. From the main test page, click on the linkfor the method TestUserPassword. You are presented with a page to input the user ID andpassword (see Figure 13.10.) If you type FSBarker for the strUserID and test for thestrPassword, the value True is returned; otherwise, False is returned. 1. Create an ASP.NET Web service project, calling it SecurityWebService. 2. Highlight the default .asmx file created in the Solution Explorer, renaming it to SecurityServices.asmx. 3. Click on the View Code button in the Solution Explorer. Change the WebService at the top of the code to read as follows: 4. _ 6. Public Class SecurityServices 7. Add the code in Listing 13.2 to the code of the Web Service. (Double-click on the Web Service to bring up the code.) You could replace the commented out lines that display for the Hello World Web method. This code starts off by specifying the description for the Web Method and then declaring the function header for the method called TestUserPassword. The parameters strUserID and strPassword are passed, and a Boolean type value is returned. The rest of this routine should looksomewhat familiar because a DataAdapter object is created, and a DataTableobject is filled, based on the username that was passed in.If a record is not found for the user, then False is passed back.If a record is found and the password matches, then True is passed back. If thepassword for the user does not match, then False is passed back.Listing 13.2 SecurityServices.asmx.vb: Web Method to Validate Usernameand PasswordPublic Function TestUserPassword(ByVal strUserID As String, _ ByVal strPassword As String) As Boolean Dim ocnn As New _ OleDb.OleDbConnection(Provider=Microsoft.Jet.OLEDB.4.0; & _ Data Source=E:InetPubwwwrootSecurityWebService & _ WebServiceSecurity.MDB) Dim odaUsers As New _ OleDb.OleDbDataAdapter(Select * from tblUsers & _Where UserID = &strUserID & , ocnn) Dim dtUsers As New DataTable() odaUsers.Fill(dtUsers) If dtUsers.Rows.Count = 0 Then TestUserPassword = False ElseIf dtUsers.Rows(0).Item(Password) = strPassword Then TestUserPassword = True Else TestUserPassword = False End IfEnd FunctionNo ...

Tài liệu được xem nhiều:

Tài liệu liên quan: