Configuring
SoapHeader to webmethods will solve your problem.
In webservices messages will be transformed through wire in SOAP format.
SOAP basically contains SOAP Envelope which is collection of
SOAP Header (The data which is sensitive and need to be transferred usually credentials, Credit card details.... etc )
SOAP Body (Main result)
SOAP Fault or Error (Error message from service)
Define a class which implements
SoapHeader and define 2 properties named UserName and Password.
ex:
public class CustomAuth: System.Web.Services.Protocols.SoapHeader
{
public string UserName;
public string Password;
}
Set the Soap Profile 1.1 to enable this security feature
Following is the service implementation
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TestService : System.Web.Services.WebService
{
public CustomAuth myTestSvcAuthentication;
[WebMethod()]
[SoapHeader("myTestSvcAuthentication",Required=true)]
public string DoSomething()
{
string _username = myTestSvcAuthentication.UserName;
string _password = myTestSvcAuthentication.Password;
if (CheckCredentials(_username, _password))
{
return "Successfully done";
}
else
{
return new Exception("Authentication failed");
}
}
private bool CheckCredentials(string uname,string pwd)
{
try
{
}
catch(Exception ex)
{
}
}
}
Client Code
using MyService;
TestService svc = new TestService();
CustomAuth customAuth= new CustomAuth();
customAuth.UserName = "Yoganand";
customAuth.Password = "Yoga@123";
svc.myTestSvcAuthentication = customAuth;
Console.WriteLine(svc.DoSomething());
For your ref:
http://msdn.microsoft.com/en-us/library/8728chd5(v=vs.80).aspx[
^]
hope it will help you.. :)
Happy Coding