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.