Monday, May 27, 2013

Base 64 Encoding in ASP.NET with C#

Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding. Base64 encoding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remain intact without modification during transport. Base64 is commonly used in a number of applications including email via MIME, and storing complex data in XML. Base64 is not encryption but simply encoding.

Base64 Encoding:

 private string base64Encode(string sData)
    {
        try
        {
            byte[] encData_byte = new byte[sData.Length];

            encData_byte = System.Text.Encoding.UTF8.GetBytes(sData);

            string encodedData = Convert.ToBase64String(encData_byte);

            return encodedData;

        }
        catch (Exception ex)
        {
            throw new Exception("Error in base64Encode" + ex.Message);
        }
    }

Base64 Decoding:

 public string base64Decode(string sData)
    {

        System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();

        System.Text.Decoder utf8Decode = encoder.GetDecoder();

        byte[] todecode_byte = Convert.FromBase64String(sData);

        int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

        char[] decoded_char = new char[charCount];

        utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);

        string result = new String(decoded_char);

        return result;

    }

Implementing in a login module:

try
        {
            var id = (from a in linq_obj.login_msts
                      select new
                      {
                          user_name = a.user_name,
                          password = a.password,
                          role = a.role,
                          int_glcode=a.int_glcode
                      }).ToList();
            int flag = 0;

            for (int i = 0; i < id.Count(); i++)
            {
                if (id[i].user_name == txtadmin.Text && txtpassword.Text == base64Decode(id[i].password) && id[i].role == "Admin")
                {
                    flag = 1;
                    Session["username1"] = txtadmin.Text;
                    Session["referense"] = id[i].int_glcode;
                    break;
                }
                else if (id[i].user_name == txtadmin.Text && txtpassword.Text == base64Decode(id[i].password) && id[i].role == "User")
                {
                    flag = 2;
                    Session["username1"] = txtadmin.Text;
                    Session["userid"] = id[i].user_name; // save a member Code
                    Session["referense"] = id[i].int_glcode;
                    //Session["code"] = id[i].login_id;
                    break;
                }
                else if (id[i].user_name == txtadmin.Text && txtpassword.Text == base64Decode(id[i].password) && id[i].role == "Super Admin")
                {
                    flag = 3;
                    Session["username1"] = txtadmin.Text;
                    Session["userid"] = id[i].user_name; // save a member Code
                    Session["referense"] = id[i].int_glcode;
                    //Session["code"] = id[i].login_id;
                    break;
                }
                else if (id[i].user_name == txtadmin.Text && txtpassword.Text == base64Decode(id[i].password) && id[i].role == "Employee")
                {
                    flag = 4;
                    Session["username1"] = txtadmin.Text;
                    Session["userid"] = id[i].user_name; // save a member Code
                    Session["referense"] = id[i].int_glcode;
                    //Session["code"] = id[i].login_id;
                    break;
                }
            }
            if (flag == 1)
            {
                Response.Redirect("admin_welcome.aspx", false);
            }
            else if (flag == 2)
            {
                Response.Redirect("changePassword.aspx", false);
            }
            else if (flag == 3)
            {
                Response.Redirect("AddAdmin.aspx", false);
            }
            else if (flag == 4)
            {
                Response.Redirect("~\\style-demo.html", false);
            }

            else
            {
                Page.RegisterStartupScript("onload", "<script language='javascript'>alert('** Incorrect UserName or Password**')</script> ");
            }
            txtadmin.Text = "";
            txtpassword.Text = "";

        }
        catch (Exception ex)
        {
            Response.Write("<script laguage='javascript'>alert('** Some Error is occured During Login**')</Script>");
        }
        finally
        {
        }

No comments: