A simple WebService implementation example, soap security verification

Source: Internet
Author: User

WebService implements network communication based on the SOAP protocol. However, this article only introduces a simple example of WebService. If you are interested, you can search for it on the website,

There are a lot of materials in this regard.

The following is an example of WebService.

1. Create a WebService project and create a soapheader. CS class under the project. This class is used for webserivce security verification. This class must inherit the soapheader class of the. NET class library.

Code List:

Using system; using system. web. services. protocols; namespace shuanglu. webService {public class mysoapheader: soapheader {public mysoapheader () {// todo: add the constructor logic here //} private string _ userid = string. empty; private string _ password = string. empty; Public String errinfo = "The resource you are looking for has been deleted, renamed, or temporarily unavailable. "; Public String userid {get {return _ userid;} set {_ userid = value ;}} Public String password {get {return _ password ;} set {_ password = value ;}} public bool isvalid () {string MSG = string. empty; return isvalid (_ userid, _ password, out MSG);} public bool isvalid (out string MSG) {return isvalid (_ userid, _ password, out MSG );} public bool isvalid (string nuserid, string npassword, out Str Ing MSG) {MSG = ""; try {If (nuserid = "19873534 Socrates" & npassword = "daling234534") {return true ;} else {MSG = "Sorry, you have no permission to call this WebService. The possible reasons are as follows: 1. Your account administrator has disabled it. 2. Your account password is incorrect! "; Return false ;}} catch (exception e) {MSG =" sorry, you are not authorized to call this WebService. The possible reasons are as follows: 1. Your account administrator has disabled it. 2. Your account password is incorrect! <Br/> "+ E. Message; return false ;}}}}

The above code list line,
If (nuserid = "19873534 Socrates" & npassword = "daling234534 ")

The user name and password submitted by the client. You can set it by yourself. The client must be consistent with the server when calling it,

2. Create a test. asmx file in the WebService project to call the WebService portal for the client,

The client must verify the call before calling WebService to check whether the call permission is available,

If you have the permission, you can call the API. Otherwise, an empty message or an error message is returned, which can be defined based on your actual situation.

Write a simple call example here.

Code List:

Using system; using system. componentmodel; using system. web. services; using system. data; using system. web. services. protocols; namespace shuanglu. webService {// <summary> /// Summary of article /// </Summary> [WebService (namespace = "http://tempuri.org/")] [webservicebinding (conformsto = wsiprofiles. basicprofile1_1)] [toolboxitem (false)] // to allow ASP. net Ajax calls this web service from the script. Please cancel the comments to the downstream. // [System. web. script. services. scriptservice] public class article: system. web. services. webService {// defines the header, but does not instantiate it. The client must instantiate public mysoapheader soapheader; [soapheader ("soapheader", direction = soapheaderdirection. inout)] [webmethod (description = "Web service call test", enablesession = true)] Public String mytest () {If (soapheader. isvalid () {string S = "Successful Web service call test"; return s ;}return "";}}}

Note that the red text in the code list above should be consistent in the two places

Public mysoapheader soapheader;

[Soapheader ("soapheader", direction = soapheaderdirection. inout)]

If they are inconsistent, an error is returned.

At this point, a simple WebService project has been created.

Next we will deploy it.

Create a site in IIS, and create a virtual directory for the XP system. Direct the root directory of the newly created site or directory to the WebService project just created,

Then use the browser to browse the WebService site or virtual directory test. asmx file just created to see if it has been correctly deployed on IIS.

After successfully deploying webserivce to IIS, we can make a simple call.


3. Create a webapplication or website project,

Add a WebService reference, right-click the project name, and the following menu is displayed. Enter the webserviceurl deployed on IIS in the following URL bar and click go.

There is a Web reference name on the right, which can be customized and used in referencing WebService. After entering the information, click

"Add reference" button to complete the WebService reference.


Create a web form file in the project, test. aspx,

Code List of the test. aspx. CS file:

Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; namespace Mike. web {public partial class test1: system. web. UI. page {protected void page_load (Object sender, eventargs e ){
Mike. Web. Article. mysoapheader soap = New Mike. Web. Article. mysoapheader (); soap. userid ="19873534 Socrates"; Soap. Password ="Daling234534"; // The username and password must be consistent with mike on each server. web. article. article BLL = New Mike. web. article. article (); BLL. mysoapheadervalue = soap; string result = BLL. mytest (); response. write (result );}}}

The above code is to call WebService. before calling the code, you must first set the user name and password in the SOAP message header, which must be consistent with those on each server to verify whether the call permission exists before calling the code.

The next step is to instantiate an instance called by WebService and assign the configured soap packet header to the attributes of the instance.

Then the following code calls the WebService method:

tring result = bll.MyTest();

Finally, the results returned by WebService are output.

The WebService, webapplication, and website are deployed on the Local Computer (or on the same computer, the Web Client can be individually debugged to the WebService ).

At this point, the whole WebService call example is complete.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.