private static QueryParameter GetNTParameter(QueryParameter queryParam, Dictionary <string, object> NTParameters)
    {
        QueryParameter NTParam = new QueryParameter();

        //รวบ NT parameter จาก queryParam
        if (queryParam != null && queryParam.Parameter != null)
        {
            foreach (KeyValuePair <string, object> p in queryParam.Parameter)
            {
                if (p.Key.IndexOf("NT_") == 0) //check ว่ามี NT_ นำหน้าคือ parameter สำหรับส่ง Notification
                {
                    NTParam.Add(p.Key, p.Value);
                }
            }
        }

        //รวบ NT parameter จาก PO ของ SP ==> PO_NT_XXXX
        if (NTParameters != null)
        {
            foreach (KeyValuePair <string, object> p in NTParameters)
            {
                //if (p.Key.IndexOf("NT_") == 0) //check ว่ามี NT_ นำหน้าคือ parameter สำหรับส่ง Notification
                //{
                NTParam.Add(p.Key, p.Value);
                //}
            }
        }

        return(NTParam);
    }
    public static bool IsTokenMatchInDatabase(string userID, string token)
    {
        if (string.IsNullOrEmpty(token))
        {
            return(false);
        }

        IDatabaseConnector dbConnector = new DatabaseConnectorClass();
        QueryParameter     queryParam  = new QueryParameter();

        queryParam.Add("USER_ID", userID);
        queryParam.Add("TOKEN", GetStringSha256Hash(token));

        QueryResult queryResult = dbConnector.ExecuteStoredProcedure("APP_Q_MATCH_TOKEN", queryParam);

        string matchStr = "";

        DataTable resultData = queryResult.DataTable;

        if (resultData != null && resultData.Rows.Count > 0 && resultData.Columns.Count > 0)
        {
            matchStr = queryResult.DataTable.Rows[0][0].ToString();
        }

        return(matchStr == "1");
    }
Exemple #3
0
        public void Test_NameParameters()
        {
            var parameters = new QueryParameter();

            parameters.Add("myname", "wee");
            parameters.Add("bar", "zod");

            Assert.Equal(2, parameters.NamedParameters.Count);
            Assert.Equal("wee", parameters.NamedParameters["myname"]);
            Assert.Equal("zod", parameters.NamedParameters["bar"]);
        }
Exemple #4
0
        public void Test_PositionalParameters()
        {
            var parameters = new QueryParameter();

            parameters.Add("wee");
            parameters.Add("zod");

            Assert.Equal(2, parameters.PostionalParameters.Count);
            Assert.Equal("wee", parameters.PostionalParameters[0]);
            Assert.Equal("zod", parameters.PostionalParameters[1]);
        }
Exemple #5
0
        public static QueryResult ForgetPassword(QueryParameter queryParameter)
        {
            QueryResult result = null;
            string      userID = queryParameter["USER_ID"].ToString();

            IDatabaseConnector dbConnector = new DatabaseConnectorClass();
            QueryParameter     param       = new QueryParameter();

            param.Add("USER_ID", userID);
            QueryResult queryResult = dbConnector.ExecuteStoredProcedure("UM_USER_Q", param);

            if (queryResult.Success)
            {
                string userEmail = queryResult.DataTable.Rows[0]["EMAIL"].ToString();
                string token     = AuthenUtil.GetStringSha256Hash(AuthenUtil.GenerateToken());

                param = new QueryParameter();
                param.Add("USER_ID", userID);
                param.Add("TOKEN", token);

                result = dbConnector.ExecuteStoredProcedure("APP_FORGET_PWD_TOKEN_I", param);

                if (result.Success)
                {
                    string passwordResetUrl = string.Format("http://localhost/WebApp/resetpassword.aspx?userID={0}&token={1}", userID, token);

                    QueryParameter mailParameter = new QueryParameter();
                    mailParameter.Add("MAIL_TO", userEmail);
                    mailParameter.Add("MAIL_SUBJECT", "Reset Password");
                    mailParameter.Add("MAIL_BODY", string.Format(@"
                        <h1>Reset Password</h1>
                        <div>
                            You have requested to reset password for account {0} <br/>
                            <b>Plase contact administrator if you have not issued reset password request.</b>
                        </div>
                        <br/>
                        Click <a href=""{1}"">here</a> to reset password.
                    ", userID, passwordResetUrl));

                    result = MailUtil.SendEmail(mailParameter);
                }
            }
            else
            {
                result         = new QueryResult();
                result.Success = false;
                result.Message = "USER_NOT_EXIST";
            }

            return(result);
        }
        public static void ClearToken(string userID, string token)
        {
            if (string.IsNullOrEmpty(token))
            {
                return;
            }

            IDatabaseConnector dbConnector = new DatabaseConnectorClass();
            QueryParameter     queryParam  = new QueryParameter();

            queryParam.Add("USER_ID", userID);
            queryParam.Add("TOKEN", GetStringSha256Hash(token));

            dbConnector.ExecuteStoredProcedure("APP_TOKEN_D", queryParam);
        }
        public static void StoreToken(string userID, string token)
        {
            if (string.IsNullOrEmpty(token))
            {
                throw new Exception("Authentication token cannot be empty");
            }

            IDatabaseConnector dbConnector = new DatabaseConnectorClass();
            QueryParameter     queryParam  = new QueryParameter();

            queryParam.Add("USER_ID", userID);
            queryParam.Add("TOKEN", GetStringSha256Hash(token));

            dbConnector.ExecuteStoredProcedure("APP_TOKEN_I", queryParam);
        }
        public static bool IsTokenMatchInDatabase(string userID, string token)
        {
            if (string.IsNullOrEmpty(token))
            {
                return(false);
            }

            IDatabaseConnector dbConnector = new DatabaseConnectorClass();
            QueryParameter     queryParam  = new QueryParameter();

            queryParam.Add("USER_ID", userID);
            queryParam.Add("TOKEN", GetStringSha256Hash(token));

            QueryResult queryResult = dbConnector.ExecuteStoredProcedure("APP_TOKEN_MATCH", queryParam);

            return(queryResult.Success);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.UrlReferrer != null)
            {
                QueryParameter queryParam = new QueryParameter(Request);

                if (queryParam.Parameter.ContainsKey("USERNAME") && queryParam.Parameter.ContainsKey("PASSWORD"))
                {
                    if (AppSession.GetSession("USER_ID") == null)
                    {
                        QueryParameter loginParam = new QueryParameter();
                        loginParam.Add("USERNAME", queryParam["USERNAME"]);
                        loginParam.Add("PASSWORD", queryParam["PASSWORD"]);
                        QueryResult queryResult = AppLogin.Login(HttpContext.Current, loginParam);
                    }
                    queryParam.Remove("USERNAME");
                    queryParam.Remove("PASSWORD");
                }
                AppSession.SetSession("REQUEST_DATA", queryParam.Parameter);
                Response.Redirect(Request.ApplicationPath);
            }
        }
Exemple #10
0
        public static QueryResult UpdateUser(QueryParameter queryParameter)
        {
            queryParameter = new QueryParameter(queryParameter.Parameter);

            if (queryParameter.Parameter.ContainsKey("PASSWORD"))
            {
                string password     = queryParameter["PASSWORD"].ToString();
                string hashPassword = AuthenUtil.GetStringSha256Hash(password);
                queryParameter.Add("PASSWORD", hashPassword);
            }

            IDatabaseConnector dbConnector = new DatabaseConnectorClass();
            QueryResult        queryResult = dbConnector.ExecuteStoredProcedure("UM_USER_U", queryParameter);

            return(queryResult);
        }
Exemple #11
0
        public static void Logout(HttpSessionState Session, HttpResponse Response = null)
        {
            IDatabaseConnector dbConnector     = new DatabaseConnectorClass();
            QueryParameter     logoutParameter = new QueryParameter();
            QueryResult        logoutResult    = new QueryResult();

            if (Session != null)
            {
                if (AppSession.GetSession("USER_ID", Session) != null)
                {
                    string userID = AppSession.GetSession("USER_ID", Session).ToString();

                    if (AppSession.GetSession("AUTHEN_TOKEN", Session) != null)
                    {
                        AuthenUtil.ClearToken(userID, AppSession.GetSession("AUTHEN_TOKEN", Session).ToString());
                    }

                    logoutParameter.Add("USER_ID", userID);
                    logoutResult         = dbConnector.ExecuteStoredProcedure("SYS_I_LOGOUT", logoutParameter);
                    logoutResult.Success = true;
                    logoutResult.Message = string.Empty;
                    logoutResult.RemoveOutputParam("error");
                }

                AppSession.SetSession("USER_ID", null, Session);
                AppSession.SetSession("AUTHEN_TOKEN", null, Session);
                AppSession.SetSession("IS_GUEST", true, Session);

                //ถ้าอยาก clear session จะต้องบังคับให้ client refresh หน้าเว็บด้วยนะ เพราะต้อง regen CSRF ด้วย
                Session.Clear();
                Session.Abandon();
            }

            if (Response != null)
            {
                HttpCookie authenTokenCookie = new HttpCookie("AUTHEN_TOKEN");
                authenTokenCookie.Value = "";
                Response.Cookies.Add(authenTokenCookie);

                //ถ้าอยาก clear session จะต้องบังคับให้ client refresh หน้าเว็บด้วยนะ เพราะต้อง regen CSRF ด้วย
                Response.Cookies["esrith.session.id"].Expires = DateTime.Now.AddDays(-30);

                Response.ClearContent();
                Response.ContentType = "application/json";
                Response.Write(logoutResult.ToJson());
            }
        }
        public static QueryResult Login(HttpContext context, QueryParameter queryParameter)
        {
            queryParameter = new QueryParameter(queryParameter.Parameter);
            if (queryParameter.Parameter.ContainsKey("PASSWORD"))
            {
                string password     = queryParameter["PASSWORD"].ToString();
                string hashPassword = AuthenUtil.GetStringSha256Hash(password);
                queryParameter.Add("PASSWORD", hashPassword);
            }

            IDatabaseConnector dbConnector = new DatabaseConnectorClass();
            QueryResult        queryResult = dbConnector.ExecuteStoredProcedure("APP_LOGIN_Q", queryParameter);

            if (queryResult.Success && queryResult.DataTable != null && queryResult.DataTable.Rows.Count > 0)
            {
                foreach (DataColumn dataColumn in queryResult.DataTable.Columns)
                {
                    foreach (AppSessionItem sessionItem in AppSession.AppSessionConfig.SessionItems)
                    {
                        if (sessionItem.Name.Equals(dataColumn.ColumnName))
                        {
                            AppSession.SetSession(dataColumn.ColumnName, queryResult.DataTable.Rows[0][dataColumn.ColumnName], context.Session);
                            break;
                        }
                    }
                }

                string userID = AppSession.GetSession("USER_ID", context.Session).ToString();
                string token  = AuthenUtil.GenerateToken();

                AppSession.SetSession("AUTHEN_TOKEN", token, context.Session);
                AppSession.SetSession("IS_GUEST", false, context.Session);

                if (AppHttpHandler.AppHttpHandlerConfig.Security.EnableDuplicateAuthenChecking)
                {
                    AuthenUtil.StoreToken(userID, token);
                }

                // ถ้าต้องการให้ retrun ค่าของ USER_ID ไปด้วยให้ลบบรรทัดนี้เลย
                queryResult.DataTable.Columns.Remove("USER_ID");
            }
            return(queryResult);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string userID = Request.Params["userID"];
            string token  = Request.Params["token"];

            if (userID != null && token != null)
            {
                IDatabaseConnector dbConnector = new DatabaseConnectorClass();
                QueryParameter     param       = new QueryParameter();
                QueryResult        queryResult = null;

                // check if userId, token is valid.

                param = new QueryParameter();
                param.Add("USER_ID", userID);
                param.Add("TOKEN", token);
                queryResult = dbConnector.ExecuteStoredProcedure("APP_FORGET_PWD_TOKEN_MATCH", param);

                if (!queryResult.Success)
                {
                    this.PasswordResetForm.Visible = false;

                    if (queryResult.Message == "TOKEN_EXPIRED")
                    {
                        this.Message.Text = "URL is expired";
                    }
                    else
                    {
                        this.Message.Text = "Unauthorized";
                    }
                }

                //if this request come form asp.net webform, then
                //  check if password == confirmpassword
                if (IsPostBack)
                {
                    string password        = this.Password.Text.Trim();
                    string passwordConfirm = this.PasswordConfirm.Text;

                    if (password.Length == 0)
                    {
                        this.Message.Text = "Password is empty";
                    }
                    else if (password != passwordConfirm)
                    {
                        this.Message.Text = "Password is not match";
                    }
                    else
                    {
                        string hashPassword = AuthenUtil.GetStringSha256Hash(password);

                        param = new QueryParameter();
                        param.Add("USER_ID", userID);
                        param.Add("PASSWORD", hashPassword);

                        queryResult = dbConnector.ExecuteStoredProcedure("UM_USER_PWD_U", param);

                        if (queryResult.Success)
                        {
                            param = new QueryParameter();
                            param.Add("USER_ID", userID);
                            queryResult = dbConnector.ExecuteStoredProcedure("APP_FORGET_PWD_TOKEN_D", param);

                            this.PasswordResetForm.Visible = false;
                            this.Message.Text = "Password reset successful. Please go to login page.";
                        }
                    }
                }
            }
            else
            {
                this.PasswordResetForm.Visible = false;
                this.Message.Text = "Unauthorized";
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                QueryParameter queryParameter = new QueryParameter(context);
                QueryResult    queryResult    = new QueryResult();

                AppHttpHandler.ProcessRequest(context, queryParameter, AuthenUtil.AuthenMode.BYPASS);

                if (context.Session != null)
                {
                    if (AppSession.GetSession("USER_ID", context.Session) != null)
                    {
                        IDatabaseConnector dbConnector = new DatabaseConnectorClass();

                        string userID  = AppSession.GetSession("USER_ID", context.Session).ToString();
                        string sqlStmt = "SELECT PASSWORD FROM UM_USER WHERE USER_ID=?";
                        queryParameter = new QueryParameter();
                        queryParameter.Add("USER_ID", userID);
                        QueryResult queryUser = dbConnector.ExecuteStatement(sqlStmt, queryParameter);
                        if (queryUser.DataTable != null && queryUser.DataTable.Rows.Count > 0)
                        {
                            queryParameter = new QueryParameter();
                            queryParameter.Add("USERNAME", userID);
                            queryParameter.Add("PASSWORD", queryUser.DataTable.Rows[0][0].ToString());
                            queryUser = dbConnector.ExecuteStoredProcedure("APP_LOGIN_Q", queryParameter);

                            queryParameter = new QueryParameter();
                            queryParameter.Add("APP_SESSION_USER_ID", userID);
                            queryResult = dbConnector.ExecuteStoredProcedure("APP_CONFIG_Q", queryParameter);

                            queryResult.AddOutputParam("userInfo", ConnectorUtil.DataTableToDictionary(queryUser.DataTable, dbConnector.DateTimeFormat, dbConnector.CultureInfo));
                        }
                    }

                    if (AppSession.GetSession("REQUEST_DATA", context.Session) != null)
                    {
                        queryResult.AddOutputParam("requestData", AppSession.GetSession("REQUEST_DATA", context.Session));
                    }
                }
                else
                {
                    throw new Exception("EMPTY_SESSION");
                }

                context.Response.ContentType = "application/json";
                context.Response.Write(queryResult.ToJson());
                context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK;

                AppHttpHandler.ProcessResponse(context);
            }
            catch (Exception exception)
            {
                AppHttpHandler.ProcessException(exception, context);
            }
            finally
            {
                context.Response.Flush();
                context.Response.End();
            }
        }