title: performing authorisation/authentication between webservices
body: <p>i am developing webservices.In that i want to maintain state information so that all WebMethods could be access only after Login.
  I have tried but getting problem.
  I am attaching my code.
  Any other alternative will also be welcomed.</p>
  
  <hr>
  
  <p>[</p>
  
  <p>WebService(Namespace = "<a href="http://amSubfah.org/" rel="nofollow noreferrer">http://amSubfah.org/</a>")] </p>
  
  <p>[</p>
  
  <p>WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] </p>
  
  <p>// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. </p>
  
  <p>// [System.Web.Script.Services.ScriptService]</p>
  
  <p>public</p>
  
  <p>class Login : System.Web.Services.WebService { </p>
  
  <p>Message msgObj = new Message(); </p>
  
  <p>BaseClass b = new BaseClass(); </p>
  
  <p>PasswordEncryptionDecryption pedObj = new PasswordEncryptionDecryption(); </p>
  
  <p>public AuthHeader Authentication=new AuthHeader (); </p>
  
  <p>public Login () { </p>
  
  <p>//Uncomment the following line if using designed components </p>
  
  <p>//InitializeComponent(); </p>
  
  <p>}</p>
  
  <p>[</p>
  
  <p>SoapHeader("Authentication", Required = true)] </p>
  
  <p>[System.Web.Services.</p>
  
  <p>WebMethod(EnableSession = true)] </p>
  
  <p>public string checkUserLogin(string user, string pwd) </p>
  
  <p>{</p>
  
  <p>DataSet dsLogin = new DataSet(); </p>
  
  <p>List sqlParams = new List(); </p>
  
  <p>SqlParameter sqlParam1 = new SqlParameter("@UserName", SqlDbType.NVarChar); </p>
  
  <p>sqlParam1.Value = user;</p>
  
  <p>sqlParams.Add(sqlParam1);</p>
  
  <p>SqlParameter sqlParam2 = new SqlParameter("@Password", SqlDbType.NVarChar); </p>
  
  <p>string pass = pedObj.encryptPassword(pwd); </p>
  
  <p>sqlParam2.Value = pass;</p>
  
  <p>sqlParams.Add(sqlParam2);</p>
  
  <p>try </p>
  
  <p>{</p>
  
  <p>b.initializeDBConnection();</p>
  
  <p>dsLogin = b.execSelectLoginQuery(</p>
  
  <p>Query.strSelectLoginData, sqlParams); </p>
  
  <p>}</p>
  
  <p>catch (SqlException sqlEx) </p>
  
  <p>{</p>
  
  <p>string str = msgObj.msgErrorMessage + sqlEx.Message + sqlEx.StackTrace; </p>
  
  <p>}</p>
  
  <p>{if ((dsLogin != null) &amp;&amp; (dsLogin.Tables[0].Rows.Count != 0)) </p>
  
  <p>{</p>
  
  <p>Session[</p>
  
  <p>"username"] = user; </p>
  
  <p>string sessionId = System.Guid.NewGuid().ToString(); </p>
  
  <p>Authentication.sessionId = sessionId;</p>
  
  <p>Authentication.Username = user;</p>
  
  <p>return msgObj.msgLoginSuccess; </p>
  
  <p>}</p>
  
  <p>else </p>
  
  <p>return msgObj .msgLoginFail ; </p>
  
  <p>}</p>
  
  <p>//webmethod for registration </p>
  
  <p>[</p>
  
  <p>SoapHeader("Authentication", Required = true)] </p>
  
  <p>[System .Web .Services . </p>
  
  <p>WebMethod (EnableSession =true )] </p>
  
  <p>public string insertRegistrationDetails(string fName,string lName,string email,string pwd) </p>
  
  <p>{</p>
  
  <p>//string u = Session["username"].ToString(); </p>
  
  <p>//if (u == "") </p>
  
  <p>//{ </p>
  
  <p>// //checkUserLogin(fName,pwd ); </p>
  
  <p>// return "Please login first"; </p>
  
  <p>//} </p>
  
  <p>if (Authentication.Username == null || Authentication.sessionId == null) </p>
  
  <p>{</p>
  
  <p>return "Please Login first"; </p>
  
  <p>}</p>
  
  <p>List sqlParams = new List(); </p>
  
  <p>int insert = 0; </p>
  
  <p>string msg = "" ; </p>
  
  <p>SqlParameter sqlParam = new SqlParameter("@FName", SqlDbType.NVarChar); </p>
  
  <p>sqlParam.Value = fName;</p>
  
  <p>sqlParam.Size = 50;</p>
  
  <p>sqlParams.Add(sqlParam);</p>
  
  <p>SqlParameter sqlParam1 = new SqlParameter("@LName", SqlDbType.NVarChar); </p>
  
  <p>sqlParam1.Value = lName;</p>
  
  <p>sqlParam1.Size = 50;</p>
  
  <p>sqlParams.Add(sqlParam1);</p>
  
  <p>SqlParameter sqlParam5 = new SqlParameter("@Email", SqlDbType.NVarChar); </p>
  
  <p>sqlParam5.Value = email;</p>
  
  <p>sqlParam5.Size = 50;</p>
  
  <p>sqlParams.Add(sqlParam5);</p>
  
  <p>SqlParameter sqlParam7 = new SqlParameter("@Password", SqlDbType.NVarChar); </p>
  
  <p>sqlParam7.Value = pedObj .encryptPassword (pwd);</p>
  
  <p>sqlParam7.Size = 50;</p>
  
  <p>sqlParams.Add(sqlParam7);</p>
  
  <p>try </p>
  
  <p>{</p>
  
  <p>b.initializeDBConnection();</p>
  
  <p>insert = b.execByKeyParams(</p>
  
  <p>Query.strInsertIntoRegistrationTable1, sqlParams); </p>
  
  <p>if (insert !=0) </p>
  
  <p>{</p>
  
  <p>msg = msgObj .msgRecInsertedSuccess ;</p>
  
  <p>}</p>
  
  <p>}</p>
  
  <p>catch (SqlException sqlEx) </p>
  
  <p>{</p>
  
  <p>string str = msgObj.msgErrorMessage + sqlEx.Message + sqlEx.StackTrace; </p>
  
  <p>}</p>
  
  <p>return msg; </p>
  
  <p>}</p>
  
  <p>public class AuthHeader : SoapHeader </p>
  
  <p>{</p>
  
  <p>public string Username; </p>
  
  <p>public string sessionId; </p>
  
  <p>}</p>
  
  <p>}</p>