I try to make asmx webservice and authenticate the user. After successfully authenication i try create session so user can perform other operations such as add person.
Problem: After successfully login when i try to addContact then get error message session is empty mean there is no session.
So here is my Webservice with three mehtod Login, Logout and addContact.
using integration.App_Code;
using System;
using System.Web.Services;
using log4net;
using System.Data;
namespace integration.services
{
[WebService(Namespace = "http://tempuri.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class upsale : System.Web.Services.WebService
{
private static readonly ILog log = LogManager.GetLogger("integration");
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod(EnableSession = true)]
public response addContact(contactPerson person)
{
try
{
response res = new response();
if (String.IsNullOrEmpty(this.Session["UserName"] as string))
{
res.statusCode = "401";
res.statusMessage = "Unauthorized";
}
else
{
res.statusCode = "200";
res.statusMessage = "OK";
}
return res;
}
catch (Exception exc)
{
log.Error("Exception in addContact: " + exc.Message);
log.Error("Exception in addContact: " + exc.StackTrace);
response res = new response();
res.createResponse("500", "Internal Server Error");
return res;
}
}
[WebMethod(EnableSession = true)]
public response login(string userName, string password)
{
try
{
response res = new response();
if (String.IsNullOrEmpty(this.Session["UserName"] as string))
{
if (String.IsNullOrEmpty(userName) || String.IsNullOrEmpty(password))
{
res.statusCode = "314";
res.statusMessage = "Empty username or password is not allowed.";
}
else if (!String.IsNullOrEmpty(userName) && !String.IsNullOrEmpty(password))
{
DbHandler handler = new DbHandler();
string sql = "Select * From systemUser Where Name='" + userName + "' and pass='" + password + "'";
log.Debug("upsale login dbQuery: " + sql);
DataTable dt = handler.dbQuery(sql);
if (dt.Rows.Count > 0)
{
if (Convert.ToBoolean(dt.Rows[0]["userStatus"].ToString()))
{
this.Session["userID"] = dt.Rows[0]["ID"].ToString().Trim();
this.Session["UserName"] = dt.Rows[0]["userName"].ToString().Trim();
res.statusCode = "200";
res.statusMessage = "OK";
}
}
}
}
else
{
res.statusCode = "200";
res.statusMessage = "Already Login";
}
return res;
}
catch (Exception exc)
{
log.Error("Exception in login: " + exc.Message);
log.Error("Exception in login: " + exc.StackTrace);
response res = new response();
res.createResponse("500", "Internal Server Error");
return res;
}
}
[WebMethod]
public response logout()
{
try
{
this.Session.Abandon();
response res = new response();
res.statusCode = "200";
res.statusMessage = "Logout Successfully";
return res;
}
catch (Exception exc)
{
log.Error("Exception in logout: " + exc.Message);
log.Error("Exception in logout: " + exc.StackTrace);
response res = new response();
res.createResponse("500", "Internal Server Error");
return res;
}
}
}
}
Client:
protected void btnAction_Click(object sender, EventArgs e)
{
try
{
upsaleClient.upsaleSoapClient client = new upsaleClient.upsaleSoapClient();
upsaleClient.contactPerson contactPerson = new upsaleClient.contactPerson();
upsaleClient.response res = new upsaleClient.response();
contactPerson.ContactPersonName = "Dummy Contact Person";
contactPerson.ContactPersonEmail = "dummy@abc.com";
res = client.addContact(contactPerson);
lbResponse.Text = "Response Status Code: " + res.statusCode + " Response Status Message: " + res.statusMessage;
}
catch (Exception exc)
{
lbResponse.Text = exc.Message;
}
}
protected void btnLogin_Click(object sender, EventArgs e)
{
try
{
upsaleClient.upsaleSoapClient client = new upsaleClient.upsaleSoapClient();
upsaleClient.response res = new upsaleClient.response();
res = client.login("userName", "password");
lbResponse.Text = "Response Status Code: " + res.statusCode + " Response Status Message: " + res.statusMessage;
}
catch (Exception exc)
{
lbResponse.Text = exc.Message;
}
}
According to your description and codes, I guess there is something wrong with your custom reponse.
Could you please post more details information about how you define your reponse class?
As far as I know, if you want to set the session ,you should return session id to client side and the client will store the session into the cookie.
Best Regards,
Brando
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
297 Points
940 Posts
ASMX session management
Jul 01, 2019 10:11 PM|shahid.majeed|LINK
Hi,
I try to make asmx webservice and authenticate the user. After successfully authenication i try create session so user can perform other operations such as add person.
Problem: After successfully login when i try to addContact then get error message session is empty mean there is no session.
So here is my Webservice with three mehtod Login, Logout and addContact.
Client:
/Shahid
Star
9831 Points
3120 Posts
Re: ASMX session management
Jul 02, 2019 02:28 AM|Brando ZWZ|LINK
Hi Shahid Majeed,
According to your description and codes, I guess there is something wrong with your custom reponse.
Could you please post more details information about how you define your reponse class?
As far as I know, if you want to set the session ,you should return session id to client side and the client will store the session into the cookie.
Best Regards,
Brando
Member
297 Points
940 Posts
Re: ASMX session management
Jul 02, 2019 08:17 AM|shahid.majeed|LINK
Hi Brando,
Response class is very simple class just hold statusCode and statusMessage properties.
Member
297 Points
940 Posts
Re: ASMX session management
Jul 05, 2019 11:29 AM|shahid.majeed|LINK
Hi,
I figureout i havent enable allowcookies in the web. after enabled allowcookies client manage session.