Asp. WebService security mechanism based on soaphead in net-practical skills

Source: Internet
Author: User
Tags auth soap

Using the Soaphead method, you can add header information to the WebService request, and when someone calls our WebService, you can check the header information of the request and verify that it prevents the program from being invoked outside of the software webservice

One, the service end part

Using System;
Using System.Web.Services;

Using System.Web.Services.Protocols; 
Note that this namespace must be distinct from the namespace on the agent dynamic Connection library. 
Otherwise, there will be an error such as a multiple-place definition Authheader.  namespace Soapheaderscs {//SoapHeader Extended Authheader class public class Authheadercs:soapheader {public string
    Username;
  public string Password;

    }//[webservice (description= "A simple example for demonstrating the usage of SOAP headers")] public class Headerservice {public Authheadercs sheader;
    [WebMethod (Description = "This method requires a SOAP header file with the caller's custom settings")] [SoapHeader ("Sheader")] public string SecureMethod () {if (Sheader = = null) return "ERROR: You are not a VIP user!

      ";
      string usr = sheader.username;

      string pwd = Sheader.password;
      if (Authenticateuser (usr, pwd)) {return "success:" + usr + "," + pwd;
      else {return "error: failed to authenticate"; } private bool Authenticateuser (string usr, string pwd) {if (usr!= null) && (pwd!= null)
      ) {return true;
}      return false;

 }
  }
}

Second, the client part plus the authentication request

WebService WebService = new WebService ();
Authheadercs auth = new Authheadercs ();
Auth. Username = "VIP";
Auth. Password = "VIPPW";
WebService. Authheadercsvalue = auth;
TextBox1.Text = WebService. SecureMethod ();

The above is based on Soaphead WebService security mechanism of all content, I hope to give you a reference, but also hope that we support the cloud habitat community.

Related Article

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.