Tuesday 10 April 2012

How to : Encrypt and Decrypt string using Asp.net

Introduction

Many time we need to encrypt and decrypt values to send over the query sting  and prevent the attacks.


below are the function we can use to accomplish those task

need to add namespace as below

using System.Security.Cryptography;



public static string EncryptText(string Text)
        {
            string Key = "@()$*#-=1";
            return Encrypt(Text, Key);
        }
        public static string DecryptText(string Text)
        {
            string Key = "@()$*#-=1";
            return Decrypt(Text, Key);
        }
        private static string Encrypt(string strText, string strEncrKey)
        {
            byte[] byKey = { };
            byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };

            byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());



        }
        private static string Decrypt(string strText, string sDecrKey)
        {
            byte[] byKey = { };
            byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
            byte[] inputByteArray = new byte[strText.Length + 1];

            byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(strText);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            System.Text.Encoding encoding = System.Text.Encoding.UTF8;
            return encoding.GetString(ms.ToArray());

        }

I hope this article was useful and I thank you for viewing it. keep visiting blog , you can get more stuff

Tuesday 3 April 2012

How to : Get datatable values using jquery from client to server side.

Introduction

First you need to make call from client side to server using below javascript funtion


$.ajax({
                   type: "get",
                   url: "?ajax_mode=GetMyData,
                   dataType: "json",
                   async: false,
                   cache: false,
                   success: function(data, textStatus) {
                       if (data.status == "success") {
                          for (var row, i = 0; row = data.obj[i]; i++) {                               
                                // Process row var for as per usage.
                }
                       }
                       else {

                       }
                   }
               });

Now You need to create class for datatable and you need to define Structure for BasicOuputObject. object of this BasicOuputObject

is sent over from server side to client. as below

VB.net

Class MyData
    Public ID As Integer
    Public Name As String
    Public ContactNo As String
End Class


Public Structure BasicOuputObject
    Dim status As String
    Dim errorMessage As String
    Dim strMessage As String
    Dim obj As Object
End Structure


C#.net

class MyData
{
    public int ID;
    public string Name;
    public string ContactNo;
}
public struct BasicOuputObject
{
    public string status;
    public string errorMessage;
    public string strMessage;
    public object obj;
}

Now you have to use below function for getting datatable in code behind file.

VB.NET
Public Sub GetMyData()

        Dim objOutput As New BasicOuputObject
        Dim objCommand As New SqlCommand
        Dim lstMyData As New List(Of MyData)
        Dim objMyData As MyData
        Dim objConn As New SqlConnection("Pass Connection String")
        objCommand.CommandText = "select ID ,Name ,ContactNo from Customers"
        objCommand.Connection = objConn

        objConn.Open()
        Dim m_rdrReader As SqlDataReader = objCommand.ExecuteReader()
        If m_rdrReader.HasRows Then
            While m_rdrReader.Read()
                objMyData = New MyData
                objMyData.ID = m_rdrReader(0)
                objMyData.Name = m_rdrReader(1)
                objMyData.ContactNo = m_rdrReader(2)
                lstMyData.Add(objMyData)
            End While
        End If
        m_rdrReader.Close()

        objOutput.errorMessage = ""
        objOutput.obj = lstMyData
        objOutput.strMessage = "success"

        Dim objSerialiser As New System.Web.Script.Serialization.JavaScriptSerializer
        HttpContext.Current.Response.Write(objSerialiser.Serialize(objOutput))
        HttpContext.Current.Response.End()

    End Sub


End Class


I hope this article was useful and I thank you for viewing it. keep visiting blog , you can get more stuff






Monday 2 April 2012

How to : Get asp:label value using jquery from client to server side

Introduction

 You can access label value using any event like button client click



<asp:Label ID="lbltext" runat="server" CssClass="cssTextLabel" Text="Test"></asp:Label>
<asp:Button ID="btnGetLabelData" runat="server" OnClientClick="GetData()" /> 


define javascript function like below.
<script type="text/javascript">
   function GetData() {
            var lbltxt = $.find('span.cssTextLabel')[0].innerHTML            
            __doPostBack('GET_DATA', lbltxt);

        }
</script>

handle postback in page load of page as below.

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strLblData As String = String.Empty
        If Request("__EVENTTARGET") = "GET_DATA" Then
            strLblData = Request("__EVENTARGUMENT").ToString()
            Response.Write(strLblData)
        End If
    End Sub

C#.Net

protected void Page_Load(object sender, System.EventArgs e)
{
    string strLblData = string.Empty;
    if (Request("__EVENTTARGET") == "GET_DATA") {
        strLblData = Request("__EVENTARGUMENT").ToString();
        Response.Write(strLblData);
    }
}
I hope this article was useful and I thank you for viewing it. keep visiting blog , you can get more stuff.