Amazon Ad

Thursday, 31 August 2017

How to maintain session in asp.net

Dear All,

I faced a strange problem of maintaining session. We all know that we can maintain session and its timeout explicitly. Although we have this facility, but sometimes due to network disturbance the session gets expired and user has to come on the login or "yellow screen" again. Today, I am going to tell you how we can tackle this problem.

Step 1: I am taking one page which is dependent upon session. This page has a simple jquery code which calls a page through "HeartBeat" function. This function calls the page and sends the session in encrypted format which is again restored on the page. Thus it allows creation of session incase of any network fault or disturbance. Below is the code for the same.

in aspx html is

//your html code

//javascript code
<script src="Scripts/jquery-1.8.2.min.js"></script>
    <script type="text/javascript">
        var sess = '<%=EncryptSession(Session["LoginName"].ToString())%>';
        function HeartBeat() {
            $.post("SessionPage.aspx", { 'sess': sess }, function (data) {
            });
        }
        setInterval(HeartBeat, 5000);
    </script>

in .cs file

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Session["SessionName"] = "testsession";
            }
            else
            {
                string sessionval = Session["SessionName"].ToString();
            }
        }

        public static string passwordEncrypt(string inText, string key)
        {
            byte[] bytesBuff = Encoding.Unicode.GetBytes(inText);
            using (Aes aes = Aes.Create())
            {
                Rfc2898DeriveBytes crypto = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                aes.Key = crypto.GetBytes(32);
                aes.IV = crypto.GetBytes(16);
                using (MemoryStream mStream = new MemoryStream())
                {
                    using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cStream.Write(bytesBuff, 0, bytesBuff.Length);
                        cStream.Close();
                    }
                    inText = Convert.ToBase64String(mStream.ToArray());
                }
            }
            return inText;
        }

      
        public static string EncryptSession(String s)
        {
            string enc = passwordEncrypt(s,"secretkeyword");
            return enc;
        }

Step 2: In another page named SessionPage.aspx, I have

In .cs file
 public partial class SessionPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String sess = Request.Params["sess"].ToString();
    //This line restores the breaked session again
            Session["SessionName"] = passwordDecrypt(sess,"secretkeyword");
            Response.Write("Any message if you want to be returned");
            Response.End();
        }

        public static string passwordDecrypt(string cryptTxt, string key)
        {
            cryptTxt = cryptTxt.Replace(" ", "+");
            byte[] bytesBuff = Convert.FromBase64String(cryptTxt);
            using (Aes aes = Aes.Create())
            {
                Rfc2898DeriveBytes crypto = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                aes.Key = crypto.GetBytes(32);
                aes.IV = crypto.GetBytes(16);
                using (MemoryStream mStream = new MemoryStream())
                {
                    using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cStream.Write(bytesBuff, 0, bytesBuff.Length);
                        cStream.Close();
                    }
                    cryptTxt = Encoding.Unicode.GetString(mStream.ToArray());
                }
            }
            return cryptTxt;
        }
    }

Thanks


How to implement Captcha v3 in ASP.NET

 I was facing an issue of dom parsing in my website. I finally resolved it by using Google Captcha V3. Step 1: Get your keys from https:...