locked
ASMX session management RRS feed

  • Question

  • User1487175000 posted

    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.

    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;
                }
            }
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="integrationClient.Default" EnableSessionState="True"%>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <table>
            <tr>
                <td>
                    <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
                    &nbsp;
                    <asp:Button ID="btnAction" runat="server" Text="Add Person" OnClick="btnAction_Click" />
                    &nbsp;
                    <asp:Button ID="btnLogout" runat="server" Text="Logout" OnClick="btnLogout_Click" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lbResponse" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
        </div>
        </form>
    </body>
    </html>
    

    /Shahid

    Monday, July 1, 2019 10:11 PM

Answers

  • User1487175000 posted

    Hi,

    I figureout i havent enable allowcookies in the web. after enabled allowcookies client manage session.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 5, 2019 11:29 AM

All replies

  • User283571144 posted

    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

    Tuesday, July 2, 2019 2:28 AM
  • User1487175000 posted

    Hi Brando,

    Response class is very simple class just hold statusCode and statusMessage properties.

    namespace integration.App_Code
    {
        public class response
        {
            public string statusCode;
            public string statusMessage;
            public response()
            {
                statusCode = "200";
                statusMessage = "OK";
            }
            public void createResponse(string code, string message)
            {
                this.statusCode = code;
                this.statusMessage = message;
            }
        }
    }

    Tuesday, July 2, 2019 8:17 AM
  • User1487175000 posted

    Hi,

    I figureout i havent enable allowcookies in the web. after enabled allowcookies client manage session.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 5, 2019 11:29 AM