示例#1
0
    protected bool createIdentificationCookie(int cntval, string Role)
    {
        string id = string.Format("Id: {0} Uri: {1}", Guid.NewGuid(), HttpContext.Current.Request.Url);

        using (Utils utility = new Utils())
        {
            utility.MethodStart(id, System.Reflection.MethodBase.GetCurrentMethod());
        }
        try
        {
            string cookieCode          = Bill_Sys_Utility.GenerateOtp(6);
            string cookieEncryptionKey = System.Configuration.ConfigurationManager.AppSettings.Get("COOKIE_ENCRYPT_KEY");
            string encryptedCookieCode = Bill_Sys_Utility.ComputeHMACSHA1(cookieCode, cookieEncryptionKey);


            HttpCookie cookie2 = Request.Cookies["GREENBILLS_DETECTION"];


            cookie2.Expires = DateTime.Now.AddYears(1);

            cookie2[cntval.ToString()] = encryptedCookieCode;

            Response.Cookies.Add(cookie2);


            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now, DateTime.Now.AddMinutes(30), true, Role, FormsAuthentication.FormsCookiePath);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
            if (ticket.IsPersistent)
            {
                cookie.Expires = ticket.Expiration;
            }
            Response.Cookies.Add(cookie);

            if (UpdateCookieCode(txtUserName.Text, cookieCode))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        catch (Exception ex)
        {
            Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
            using (Utils utility = new Utils())
            {
                utility.MethodEnd(id, System.Reflection.MethodBase.GetCurrentMethod());
            }
            string str2 = "Error Request=" + id + ".Please share with Technical support.";
            base.Response.Redirect("Bill_Sys_ErrorPage.aspx?ErrMsg=" + str2);
            return(false);
        }
        //Method End
        using (Utils utility = new Utils())
        {
            utility.MethodEnd(id, System.Reflection.MethodBase.GetCurrentMethod());
        }
    }
示例#2
0
    protected bool createIdentificationCookie(string role)
    {
        string id = string.Format("Id: {0} Uri: {1}", Guid.NewGuid(), HttpContext.Current.Request.Url);

        using (Utils utility = new Utils())
        {
            utility.MethodStart(id, System.Reflection.MethodBase.GetCurrentMethod());
        }
        try
        {
            string cookieCode          = Bill_Sys_Utility.GenerateOtp(6);
            string cookieEncryptionKey = System.Configuration.ConfigurationManager.AppSettings.Get("COOKIE_ENCRYPT_KEY");
            string encryptedCookieCode = Bill_Sys_Utility.ComputeHMACSHA1(cookieCode, cookieEncryptionKey);


            if (Request.Cookies["GREENBILLS_DETECTION"] == null)
            {
                HttpCookie cookieCnt = new HttpCookie("LOGIN_COUNT", "0");
                cookieCnt.Expires = DateTime.Now.AddYears(1);
                cookieCnt.Value   = "0";
                Response.Cookies.Add(cookieCnt);

                HttpCookie cookie1 = new HttpCookie("GREENBILLS_DETECTION");

                cookie1.Expires          = DateTime.Now.AddYears(1);
                cookie1[cookieCnt.Value] = encryptedCookieCode;
                Response.Cookies.Add(cookie1);
            }
            else
            {
                HttpCookie cookie2   = Request.Cookies["GREENBILLS_DETECTION"];
                HttpCookie cookieCnt = Request.Cookies["LOGIN_COUNT"];
                if (cookieCnt == null)
                {
                    cookieCnt         = new HttpCookie("LOGIN_COUNT", "0");
                    cookieCnt.Value   = (Convert.ToInt32(cookie2.Values.Count) - 1).ToString();
                    cookieCnt.Expires = DateTime.Now.AddYears(1);
                }


                int allowCount = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings.Get("MAX_USER_ALLOW"));
                if (Convert.ToInt32(cookieCnt.Value) < allowCount - 1)
                {
                    cookie2.Expires          = DateTime.Now.AddYears(1);
                    cookieCnt.Value          = (Convert.ToInt32(cookieCnt.Value) + 1).ToString();
                    cookie2[cookieCnt.Value] = encryptedCookieCode;

                    Response.Cookies.Add(cookie2);
                    Response.Cookies.Add(cookieCnt);
                }
                else
                {
                    return(createIdentificationCookie(GetLastIndex(), role));
                }
            }



            if (UpdateCookieCode(txtUserName.Text, cookieCode))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        catch (Exception ex)
        {
            Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
            using (Utils utility = new Utils())
            {
                utility.MethodEnd(id, System.Reflection.MethodBase.GetCurrentMethod());
            }
            string str2 = "Error Request=" + id + ".Please share with Technical support.";
            base.Response.Redirect("Bill_Sys_ErrorPage.aspx?ErrMsg=" + str2);
            return(false);
        }
        //Method End
        using (Utils utility = new Utils())
        {
            utility.MethodEnd(id, System.Reflection.MethodBase.GetCurrentMethod());
        }
    }
示例#3
0
    public int GetLastIndex()
    {
        string id = string.Format("Id: {0} Uri: {1}", Guid.NewGuid(), HttpContext.Current.Request.Url);

        using (Utils utility = new Utils())
        {
            utility.MethodStart(id, System.Reflection.MethodBase.GetCurrentMethod());
        }
        int           ireturn   = 0;
        StringBuilder sqlString = new StringBuilder();

        strsqlCon = ConfigurationManager.AppSettings["Connection_String"].ToString();
        SqlConnection con = new SqlConnection(strsqlCon);

        con.Open();
        SqlCommand command = new SqlCommand();

        try
        {
            command.Connection = con;
            sqlString.Append("select sz_detect_code, SZ_USER_NAME, DT_LAST_LOGIN from MST_USERS  where ISNULL(sz_detect_code,'')<>''");
            command.CommandType = CommandType.Text;
            command.CommandText = sqlString.ToString();
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet        ds      = new DataSet();
            adapter.Fill(ds);
            command.Parameters.Clear();
            command.Dispose();

            HttpCookie cookie = Request.Cookies["GREENBILLS_DETECTION"];

            int       iCount        = 0;
            ArrayList arrDetectCode = new ArrayList();
            foreach (string val in cookie.Values)
            {
                int iFlag = 0;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (cookie[val].Equals(Bill_Sys_Utility.ComputeHMACSHA1(ds.Tables[0].Rows[i]["sz_detect_code"].ToString(), System.Configuration.ConfigurationManager.AppSettings.Get("COOKIE_ENCRYPT_KEY"))))
                    {
                        iFlag = 1;
                        arrDetectCode.Add(ds.Tables[0].Rows[i]["sz_detect_code"].ToString());
                    }
                }
                if (iFlag == 0)
                {
                    return(iCount);
                }

                iCount++;
            }



            string detectCode = string.Empty;
            for (int i = 0; i < arrDetectCode.Count; i++)
            {
                if (detectCode == string.Empty)
                {
                    detectCode = "'" + arrDetectCode[i].ToString() + "'";
                }
                else
                {
                    detectCode += ",'" + arrDetectCode[i].ToString() + "'";
                }
            }
            sqlString.Remove(0, sqlString.Length);
            sqlString.Append("select    sz_detect_code from MST_USERS where  DT_LAST_LOGIN in (");
            sqlString.Append("select  MIN(DT_LAST_LOGIN) from MST_USERS where  sz_detect_code in(" + detectCode + "))");
            sqlString.Append("and sz_detect_code in(" + detectCode + ")");


            con.Open();
            command             = new SqlCommand();
            command.CommandType = CommandType.Text;
            command.CommandText = sqlString.ToString();
            SqlDataReader dr   = command.ExecuteReader();
            string        code = "";
            while (dr.Read())
            {
                code = dr[0].ToString();
            }
            dr.Close();
            command.Parameters.Clear();
            command.Dispose();
            if (code == "")
            {
                return(ireturn);
            }
            else
            {
                foreach (string val in cookie.Values)
                {
                    if (cookie[val].Equals(Bill_Sys_Utility.ComputeHMACSHA1(code, System.Configuration.ConfigurationManager.AppSettings.Get("COOKIE_ENCRYPT_KEY"))))
                    {
                        return(ireturn);
                    }
                    ireturn++;
                }
            }
        }
        catch (Exception ex)
        {
            lblErrorMsg.Text = "Error to save.";
            Response.Clear();

            Response.Redirect("Bill_Sys_Login.aspx");
            Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
            using (Utils utility = new Utils())
            {
                utility.MethodEnd(id, System.Reflection.MethodBase.GetCurrentMethod());
            }
            string str2 = "Error Request=" + id + ".Please share with Technical support.";
            base.Response.Redirect("Bill_Sys_ErrorPage.aspx?ErrMsg=" + str2);
        }
        finally
        {
        }
        return(0);

        //Method End
        using (Utils utility = new Utils())
        {
            utility.MethodEnd(id, System.Reflection.MethodBase.GetCurrentMethod());
        }
    }