Ejemplo n.º 1
0
    /// <summary>
    /// ดึงค่า Session จาก Index ที่กำหนด
    /// </summary>
    /// <param name="SessionName">ชื่อ Session ที่ใช้เก็บการล็อคอิน</param>
    /// <param name="ArrayIndex">Index ของตัวแปรที่ต้องการ</param>
    /// <returns></returns>
    private string GetLoginSession(string SessionName, int ArrayIndex)
    {
        clsDefault clsDefault = new clsDefault();
        clsSQL clsSQL = new clsSQL();
        StringBuilder strSQL = new StringBuilder();
        string rtnValue = "";

        if (System.Web.HttpContext.Current.Session[SessionName] != null)
        {
            #region Find Session Login Value
            string[] arrLogin = System.Web.HttpContext.Current.Session[_sessionName].ToString().Split(_sessionSeparate, StringSplitOptions.None);

            if (ArrayIndex < arrLogin.Count())
            {
                rtnValue = arrLogin[ArrayIndex];
            }
            #endregion
        }
        else
        {
            #region No Session
            string strCookie;
            DataTable dt = new DataTable();

            if (clsDefault.CookieChecker(_sessionName, out strCookie))
            {
                strCookie = Decrypt(strCookie);

                #region SQL Query
                strSQL.Append("SELECT ");
                strSQL.Append("[User].UID,");
                strSQL.Append("[User].Username,");
                strSQL.Append("UserGroup.Name AS UserGroupName,");
                strSQL.Append("ISNULL(UserGroup.Authority,'') AS GroupAuthority,");
                strSQL.Append("ISNULL([User].Authority,'') AS UserAuthority ");
                strSQL.Append("FROM ");
                strSQL.Append("[User] ");
                strSQL.Append("INNER JOIN UserGroup ");
                strSQL.Append("ON [User].UserGroupUID=UserGroup.UID AND UserGroup.Active='1' ");
                strSQL.Append("WHERE ");
                strSQL.Append("[User].UID=" + _parameterChar + "UID ");
                strSQL.Append("AND [User].Active='1'");
                #endregion

                dt = clsSQL.Bind(
                    strSQL.ToString(),
                    new string[,] { { "" + _parameterChar + "UID", strCookie } },
                    _dbType,
                    _cs
                );

                if (dt != null && dt.Rows.Count > 0)
                {
                    SetLoginSession(
                        _sessionName, 
                        new string[] { 
                            strCookie, 
                            dt.Rows[0]["Username"].ToString(), 
                            dt.Rows[0]["UserGroupName"].ToString(),
                            dt.Rows[0]["GroupAuthority"].ToString(), 
                            dt.Rows[0]["UserAuthority"].ToString()
                        }
                    );

                    if (System.Web.HttpContext.Current.Session[_sessionName] != null)
                    {
                        #region Find Session Login Value
                        string[] arrLogin = System.Web.HttpContext.Current.Session[_sessionName].ToString().Split(_sessionSeparate, StringSplitOptions.None);

                        if (ArrayIndex < arrLogin.Count())
                        {
                            rtnValue = arrLogin[ArrayIndex];
                        }
                        #endregion
                    }
                }
                else
                {
                    LoginDelete();
                }
            }
            #endregion
        }

        return rtnValue;
    }
Ejemplo n.º 2
0
    /// <summary>
    /// ใช้ตรวจสอบสถานะสมาชิก จาก Session และ Cookie
    /// </summary>
    /// <param name="GroupName">ชื่อสถานะที่ต้องการตรวจสอบ</param>
    /// <param name="CreateSession">กรณีพบ Cookie ให้สร้าง Session ด้วยเลยไหม</param>
    /// <returns>true = พบข้อมูลการล็อคอิน , false = ไม่พบข้อมูลการล็อคอิน</returns>
    /// <example>
    /// clsSecurity.LoginChecker("admin");
    /// clsSecurity.LoginChecker();
    /// </example>
    public bool LoginChecker(string GroupName = "", bool CreateSession = true)
    {
        bool rtnValue = false;

        clsDefault clsDefault = new clsDefault();
        clsSQL clsSQL = new clsSQL();
        StringBuilder strSQL = new StringBuilder();

        #region Session
        if (HttpContext.Current.Session[_sessionName] != null)
        {
            if (!string.IsNullOrEmpty(GroupName))
            {
                if (GetLoginSession(_sessionName,_sessionGroup).ToLower() == GroupName.ToLower())
                {
                    rtnValue = true;
                }
                else
                {
                    rtnValue = false;
                }
            }
            else
            {
                rtnValue = true;
            }
        }
        #endregion
        #region No Session Check Cookie
        else
        {
            string strCookie;
            DataTable dt = new DataTable();

            if (clsDefault.CookieChecker(_sessionName, out strCookie))
            {
                strCookie = Decrypt(strCookie);

                #region SQL Query
                strSQL.Append("SELECT ");
                strSQL.Append("[User].UID,");
                strSQL.Append("[User].Username,");
                strSQL.Append("UserGroup.Name AS UserGroupName,");
                strSQL.Append("ISNULL(UserGroup.Authority,'') AS GroupAuthority,");
                strSQL.Append("ISNULL([User].Authority,'') AS UserAuthority ");
                strSQL.Append("FROM ");
                strSQL.Append("[User] ");
                strSQL.Append("INNER JOIN UserGroup ");
                strSQL.Append("ON [User].UserGroupUID=UserGroup.UID AND UserGroup.Active='1' ");
                strSQL.Append("WHERE ");
                strSQL.Append("[User].UID=" + _parameterChar + "UID ");
                strSQL.Append("AND [User].Active='1'");
                #endregion

                dt = clsSQL.Bind(
                    strSQL.ToString(),
                    new string[,] { { "" + _parameterChar + "UID", strCookie } },
                    _dbType,
                    _cs
                );

                if (dt != null && dt.Rows.Count > 0)
                {
                    if (!string.IsNullOrEmpty(GroupName))
                    {
                        if (dt.Rows[0]["UserGroupName"].ToString().ToLower() == GroupName.ToLower())
                        {
                            if (CreateSession)
                            {
                                SetLoginSession(
                                    _sessionName, 
                                    new string[] { 
                                        strCookie, 
                                        dt.Rows[0]["Username"].ToString(), 
                                        dt.Rows[0]["UserGroupName"].ToString(),
                                        dt.Rows[0]["GroupAuthority"].ToString(),
                                        dt.Rows[0]["UserAuthority"].ToString()
                                    }
                                );
                            }
                            rtnValue = true;
                        }
                    }
                    else
                    {
                        if (CreateSession)
                        {
                            SetLoginSession(
                                _sessionName, 
                                new string[] { 
                                    strCookie, 
                                    dt.Rows[0]["Username"].ToString(), 
                                    dt.Rows[0]["UserGroupName"].ToString(),
                                    dt.Rows[0]["GroupAuthority"].ToString(),
                                    dt.Rows[0]["UserAuthority"].ToString()
                                }
                            );
                        }
                        rtnValue = true;
                    }
                }
                else
                {
                    LoginDelete();
                }
            }
        }
        #endregion

        return rtnValue;
    }
Ejemplo n.º 3
0
    /// <summary>
    /// ใช้ตรวจสอบสถานะสมาชิก จาก Session และ Cookie
    /// </summary>
    /// <param name="GroupName">ชื่อสถานะที่ต้องการตรวจสอบ</param>
    /// <param name="CreateSession">กรณีพบ Cookie ให้สร้าง Session ด้วยเลยไหม</param>
    /// <returns>true = พบข้อมูลการล็อคอิน , false = ไม่พบข้อมูลการล็อคอิน</returns>
    /// <example>
    /// clsSecurity.LoginChecker("admin");
    /// clsSecurity.LoginChecker();
    /// </example>
    public bool LoginChecker(string GroupName = "", bool CreateSession = true)
    {
        bool rtnValue = false;

        clsDefault    clsDefault = new clsDefault();
        clsSQL        clsSQL     = new clsSQL();
        StringBuilder strSQL     = new StringBuilder();

        #region Session
        if (HttpContext.Current.Session[_sessionName] != null)
        {
            if (!string.IsNullOrEmpty(GroupName))
            {
                if (GetLoginSession(_sessionName, _sessionGroup).ToLower() == GroupName.ToLower())
                {
                    rtnValue = true;
                }
                else
                {
                    rtnValue = false;
                }
            }
            else
            {
                rtnValue = true;
            }
        }
        #endregion
        #region No Session Check Cookie
        else
        {
            string    strCookie;
            DataTable dt = new DataTable();

            if (clsDefault.CookieChecker(_sessionName, out strCookie))
            {
                strCookie = Decrypt(strCookie);

                #region SQL Query
                strSQL.Append("SELECT ");
                strSQL.Append("[User].UID,");
                strSQL.Append("[User].Username,");
                strSQL.Append("UserGroup.Name AS UserGroupName,");
                strSQL.Append("ISNULL(UserGroup.Authority,'') AS GroupAuthority,");
                strSQL.Append("ISNULL([User].Authority,'') AS UserAuthority ");
                strSQL.Append("FROM ");
                strSQL.Append("[User] ");
                strSQL.Append("INNER JOIN UserGroup ");
                strSQL.Append("ON [User].UserGroupUID=UserGroup.UID AND UserGroup.Active='1' ");
                strSQL.Append("WHERE ");
                strSQL.Append("[User].UID=" + _parameterChar + "UID ");
                strSQL.Append("AND [User].Active='1'");
                #endregion

                dt = clsSQL.Bind(
                    strSQL.ToString(),
                    new string[, ] {
                    { "" + _parameterChar + "UID", strCookie }
                },
                    _dbType,
                    _cs
                    );

                if (dt != null && dt.Rows.Count > 0)
                {
                    if (!string.IsNullOrEmpty(GroupName))
                    {
                        if (dt.Rows[0]["UserGroupName"].ToString().ToLower() == GroupName.ToLower())
                        {
                            if (CreateSession)
                            {
                                SetLoginSession(
                                    _sessionName,
                                    new string[] {
                                    strCookie,
                                    dt.Rows[0]["Username"].ToString(),
                                    dt.Rows[0]["UserGroupName"].ToString(),
                                    dt.Rows[0]["GroupAuthority"].ToString(),
                                    dt.Rows[0]["UserAuthority"].ToString()
                                }
                                    );
                            }
                            rtnValue = true;
                        }
                    }
                    else
                    {
                        if (CreateSession)
                        {
                            SetLoginSession(
                                _sessionName,
                                new string[] {
                                strCookie,
                                dt.Rows[0]["Username"].ToString(),
                                dt.Rows[0]["UserGroupName"].ToString(),
                                dt.Rows[0]["GroupAuthority"].ToString(),
                                dt.Rows[0]["UserAuthority"].ToString()
                            }
                                );
                        }
                        rtnValue = true;
                    }
                }
                else
                {
                    LoginDelete();
                }
            }
        }
        #endregion

        return(rtnValue);
    }
Ejemplo n.º 4
0
    /// <summary>
    /// ดึงค่า Session จาก Index ที่กำหนด
    /// </summary>
    /// <param name="SessionName">ชื่อ Session ที่ใช้เก็บการล็อคอิน</param>
    /// <param name="ArrayIndex">Index ของตัวแปรที่ต้องการ</param>
    /// <returns></returns>
    private string GetLoginSession(string SessionName, int ArrayIndex)
    {
        clsDefault    clsDefault = new clsDefault();
        clsSQL        clsSQL     = new clsSQL();
        StringBuilder strSQL     = new StringBuilder();
        string        rtnValue   = "";

        if (System.Web.HttpContext.Current.Session[SessionName] != null)
        {
            #region Find Session Login Value
            string[] arrLogin = System.Web.HttpContext.Current.Session[_sessionName].ToString().Split(_sessionSeparate, StringSplitOptions.None);

            if (ArrayIndex < arrLogin.Count())
            {
                rtnValue = arrLogin[ArrayIndex];
            }
            #endregion
        }
        else
        {
            #region No Session
            string    strCookie;
            DataTable dt = new DataTable();

            if (clsDefault.CookieChecker(_sessionName, out strCookie))
            {
                strCookie = Decrypt(strCookie);

                #region SQL Query
                strSQL.Append("SELECT ");
                strSQL.Append("[User].UID,");
                strSQL.Append("[User].Username,");
                strSQL.Append("UserGroup.Name AS UserGroupName,");
                strSQL.Append("ISNULL(UserGroup.Authority,'') AS GroupAuthority,");
                strSQL.Append("ISNULL([User].Authority,'') AS UserAuthority ");
                strSQL.Append("FROM ");
                strSQL.Append("[User] ");
                strSQL.Append("INNER JOIN UserGroup ");
                strSQL.Append("ON [User].UserGroupUID=UserGroup.UID AND UserGroup.Active='1' ");
                strSQL.Append("WHERE ");
                strSQL.Append("[User].UID=" + _parameterChar + "UID ");
                strSQL.Append("AND [User].Active='1'");
                #endregion

                dt = clsSQL.Bind(
                    strSQL.ToString(),
                    new string[, ] {
                    { "" + _parameterChar + "UID", strCookie }
                },
                    _dbType,
                    _cs
                    );

                if (dt != null && dt.Rows.Count > 0)
                {
                    SetLoginSession(
                        _sessionName,
                        new string[] {
                        strCookie,
                        dt.Rows[0]["Username"].ToString(),
                        dt.Rows[0]["UserGroupName"].ToString(),
                        dt.Rows[0]["GroupAuthority"].ToString(),
                        dt.Rows[0]["UserAuthority"].ToString()
                    }
                        );

                    if (System.Web.HttpContext.Current.Session[_sessionName] != null)
                    {
                        #region Find Session Login Value
                        string[] arrLogin = System.Web.HttpContext.Current.Session[_sessionName].ToString().Split(_sessionSeparate, StringSplitOptions.None);

                        if (ArrayIndex < arrLogin.Count())
                        {
                            rtnValue = arrLogin[ArrayIndex];
                        }
                        #endregion
                    }
                }
                else
                {
                    LoginDelete();
                }
            }
            #endregion
        }

        return(rtnValue);
    }
Ejemplo n.º 5
0
 /// <summary>
 /// ใช้ตรวจสอบสถานะสมาชิก จาก Session และ Cookie
 /// </summary>
 /// <param name="GroupName">ชื่อสถานะที่ต้องการตรวจสอบ</param>
 /// <param name="CreateSession">กรณีพบ Cookie ให้สร้าง Session ด้วยเลยไหม</param>
 /// <returns>true = พบข้อมูลการล็อคอิน , false = ไม่พบข้อมูลการล็อคอิน</returns>
 /// <example>
 /// clsSecurity.LoginChecker("admin");
 /// clsSecurity.LoginChecker();
 /// </example>
 public bool LoginChecker(string GroupName = "", bool CreateSession = true)
 {
     #region Variable
     var result     = false;
     var clsDefault = new clsDefault();
     var clsSQL     = new clsSQL(_dbType, _cs);
     var strSQL     = new StringBuilder();
     var strCookie  = "";
     var dt         = new DataTable();
     #endregion
     #region Procedure
     #region Session
     if (HttpContext.Current.Session[_sessionName] != null)
     {
         if (!string.IsNullOrEmpty(GroupName))
         {
             if (GetLoginSession(_sessionName, _sessionGroup).ToLower() == GroupName.ToLower())
             {
                 result = true;
             }
             else
             {
                 result = false;
             }
         }
         else
         {
             result = true;
         }
     }
     #endregion
     #region No Session Check Cookie
     else
     {
         if (clsDefault.CookieChecker(_sessionName, out strCookie))
         {
             strCookie = Decrypt(strCookie);
             #region SQLQuery
             strSQL.Append("SELECT ");
             strSQL.Append("A.UID,");
             strSQL.Append("A.Username,");
             strSQL.Append("B.Name AS UserGroupName,");
             strSQL.Append((_dbType == clsSQL.DBType.MySQL?"IFNULL":"ISNULL") + "(B.Authority,'') AS GroupAuthority,");
             strSQL.Append((_dbType == clsSQL.DBType.MySQL?"IFNULL":"ISNULL") + "(A.Authority,'') AS UserAuthority ");
             strSQL.Append("FROM ");
             strSQL.Append("[User] A ");
             strSQL.Append("INNER JOIN UserGroup B ");
             strSQL.Append("ON A.UserGroupUID=B.UID AND B.StatusFlag='A' ");
             strSQL.Append("WHERE ");
             strSQL.Append("A.UID=" + _parameterChar + "UID ");
             strSQL.Append("AND A.StatusFlag='A'");
             #endregion
             dt = clsSQL.Bind(
                 strSQL.ToString(),
                 new string[, ] {
                 { "" + _parameterChar + "UID", strCookie }
             }
                 );
             if (dt != null && dt.Rows.Count > 0)
             {
                 #region FoundData
                 if (!string.IsNullOrEmpty(GroupName))
                 {
                     if (dt.Rows[0]["UserGroupName"].ToString().ToLower() == GroupName.ToLower())
                     {
                         if (CreateSession)
                         {
                             SetLoginSession(
                                 _sessionName,
                                 new string[] {
                                 strCookie,
                                 dt.Rows[0]["Username"].ToString(),
                                 dt.Rows[0]["UserGroupName"].ToString(),
                                 dt.Rows[0]["GroupAuthority"].ToString(),
                                 dt.Rows[0]["UserAuthority"].ToString()
                             }
                                 );
                         }
                         result = true;
                     }
                 }
                 else
                 {
                     if (CreateSession)
                     {
                         SetLoginSession(
                             _sessionName,
                             new string[] {
                             strCookie,
                             dt.Rows[0]["Username"].ToString(),
                             dt.Rows[0]["UserGroupName"].ToString(),
                             dt.Rows[0]["GroupAuthority"].ToString(),
                             dt.Rows[0]["UserAuthority"].ToString()
                         }
                             );
                     }
                     result = true;
                 }
                 #endregion
             }
             else
             {
                 LoginDelete();
             }
         }
     }
     #endregion
     #endregion
     return(result);
 }