Tuesday, March 24, 2015

How to implement the single user for single login and single session in asp.net c#

How to implement the single user for single login and single session in asp.net c#

Description: In this post I want to explain how to restrict the users to login the application with one session for one login.

Example:-If one user login the application then restrict the user to login with same credentials until the session completes.

Step 1:- Create these methods
///<Authour> ANNAM LOKESH </Authour>
/// <summary>
/// Creating the single session creation using this method
/// </summary>
/// <param name="userID">User Id</param>
/// <param name="password">User password</param>
private void SingleSession(string userID, string password)
{
    Hashtable sessions = (Hashtable)Application["WEB_SESSIONS_OBJECT"];
    if (sessions == null)
    {
        sessions = new Hashtable();
        Response.Redirect("~/Home.aspx", false);
    }
    else
    {
        //check the user in the session
        if (!sessions.ContainsKey(userID))
        {
            //putting the user in the session
            sessions[userID] = userID;
            sessions[userID] = Session;
            Response.Redirect("~/Home.aspx", false);
           
        }
        else
        {
            lblError.Visible = true;
            lblError.Text = "Already Logined with LoginID/Password";
            txtCodeValidate.Text = string.Empty;
        }
    }

    //getting the pointer to the Session of the current logged in user
    HttpSessionState existingUserSession = (HttpSessionState)sessions[userID];
    if (existingUserSession != null)
    {
        existingUserSession[userID] = null;
        //logout current logged in user
    }
    //putting the user in the session
    Session[userID] = userID;
    sessions[userID] = Session;
    Application.Lock(); //lock to prevent duplicate objects
    Application["WEB_SESSIONS_OBJECT"] = sessions;
    Application.UnLock();
}
///<Authour> ANNAM LOKESH </Authour>
/// <summary>
/// Session Clearing Method using the login id
/// </summary>
/// <param name="userID"></param>
private void SingleSessionLogOut(string userID)
{
    //put your logout logic here, remove the user object from the session.
    Hashtable sessions = (Hashtable)Application["WEB_SESSIONS_OBJECT"];
    if (sessions == null)
    {
        sessions = new Hashtable();
    }

    Session.Abandon();
    sessions.Remove(userID);

    Application.Lock();
    Application["WEB_SESSIONS_OBJECT"] = sessions;
    Application.UnLock();
}




Step2:- Use that method to creation the session like
SingleSession(txtLoginID.Text.Trim(), txtPassword.Text.Trim());

Step3:- While log out access the second method to clear particular USER
SingleSessionLogOut(Session["LoginID"].ToString());
Step4:- For session out clear
Create Global.asax  
à Right click the solution explorer à Add à New Item àselect Global.asax page
protected void Session_End(object sender, EventArgs e)
{
    try
    {
        SingleSessionLogOut(Session["LoginID"].ToString());
        Response.Redirect("~/login.aspx");
    }
    catch (Exception ex)
    {

    }

}


No comments:

Post a Comment