The soaphead method can be used to add header information to the WebService request. When someone calls our WebService, it can query the header information of the request and verify it to prevent programs other than the software from calling WebService.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. Services;
Using system. Data;
Using GB. BLL;
Using GB. utility;
Using system. Web. Services. Protocols;
Public class authheadergb: soapheader
{
Public String username;
Public String password;
}
/// <Summary>
/// Summary of userservice
/// </Summary>
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
// To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream.
[System. Web. Script. Services. scriptservice]
Public class userservice: system. Web. Services. WebService
{
Public authheadergb sheader;
Public userservice ()
{
// If you use the designed component, uncomment the following line
// Initializecomponent ();
}
[Webmethod (description = "getting user information")]
[Soapheader ("sheader")]
Public datatable getcompanyinfo (string username)
{
If (sheader = NULL)
{
Return NULL;
}
Else
{
String USR = sheader. Username;
String Pwd = sheader. Password;
If (authenticateuser (USR, PWD ))
{
Companybll = new companybll ();
Datatable dt = companybll. getcompanyinfo (username );
Return DT;
}
Else
{
Return NULL;
}
}
}
Private bool authenticateuser (string USR, string PWD)
{
If (USR! = NULL & PWD! = NULL) & (USR = "gbwebservice" & Pwd = "gbpwd "))
{
Return true;
}
Return false;
}
}
Call
WebService = new WebService ();
Authheadergb auth = new authheadergb ();
Auth. Username = "gbwebservice ";
Auth. Password = "gbpwd ";
WebService. authheadergbvalue = auth;
Datatable dt = Auth. getcompanyinfo ("test ");