Answered by:
How can I maintain session value into WebService?

Question
-
I'm currently working with a web service in which I want to keep track of individual sessions, i.e. for each user.
The code that I have in my web service is as follows...
public class Service : System.Web.Services.WebService
{
public Service()
{
}
bool IsAuthenticated
{
get
{
object state = Session["IsAuthenticated"];
if (state != null)
{
return (bool)state;
}
return false;
}
set
{
Session["IsAuthenticated"] = value;
}
}
[WebMethod(EnableSession = true)]
public bool LogIn(string userName, string password)
{
// UserManager userManager = new UserManager();
bool authenticated;
authenticated = Authenticate(userName, password);
// userManager.Authenticate(userName, password);
IsAuthenticated = authenticated;
return authenticated;
}
[WebMethod(EnableSession = true)]
public void LogOut()
{
IsAuthenticated = false;
}
[WebMethod(EnableSession = true)]
public int Add(int num1, int num2)
{
if (IsAuthenticated == false)
{
throw new UnauthorizedAccessException("Invalid user name or password provided");
}
return num1 + num2;
}
[WebMethod(EnableSession = true)]
public bool Authenticate(string userName, string password)
{
if (userName == "Goyal" && password == "Goyal")
{
return true;
}
else
{
return false;
}
}
}
and aspx page code is as follow
Security.Service obj;
System.Net.CookieContainer container = new System.Net.CookieContainer();
protected void Page_Load(object sender, EventArgs e)
{
obj = new Security.Service();
obj.CookieContainer = new CookieContainer();
Session["obj"] = obj;
}
protected void btnAdd_Click(object s, EventArgs e)
{
try
{
obj = (Security.Service)Session["obj"];
bool test = obj.LogIn("Goyal", "Goyal");
int sum = obj.Add(Convert.ToInt32(txtFirstNumber.Text), Convert.ToInt32(txtSecondNumber.Text));
Response.Write(sum);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
protected void btnCancel_Click(object s, EventArgs e)
{
}
protected void lnkLogOut_Click(object s, EventArgs e)
{
Security.Service obj = new Security.Service();
obj.LogOut();
lnkLogOut.Text = "Login";
}
It is working fine but it is only Issue that LOGIN FUNCTION AND ADD FUNCTION call at same
time ...
I donot want like this ...... I want call LOGIN function on defferent Click and add function
call on different click if auntication is created after the login function....
Its an issue if I call different event fire on different button click then authentication is losted and error is come ...
How I can maintain session value or authentication of user ..... Please help me out ...........
Thanks in advance.......................... ................Narinder Goyal...............
Thursday, October 29, 2009 10:31 AM
Answers
-
Hello,
Thank you for your post! I would suggest creating a new thread for your question in the (.NET Framework Developer Center > .NET Development Forums > ASMX Web Services and XML Serialization ) forum located here: (http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/threads).
Hope that would be helpful.
Have a great day!
Thanks & regards,
Ashawani Tier 2 Application Support Server and Tools Online Engineering Live Services Team- Proposed as answer by ashawani_dubey Thursday, October 29, 2009 12:30 PM
- Marked as answer by Narinder goyal Tuesday, November 3, 2009 9:36 AM
- Unmarked as answer by Narinder goyal Tuesday, November 3, 2009 9:36 AM
- Marked as answer by ashawani_dubey Thursday, December 10, 2009 9:02 AM
Thursday, October 29, 2009 12:30 PM