Thursday, July 3, 2014

EMAIL LINK TO OTHER PAGE

1) FORM THE URL

This method is to get the current URL
1) http://localhost:64591/
2) http://sit/test/

public string GetWebAppRoot()
{
  if (HttpContext.Current.Request.ApplicationPath == "/")
    return "http://" + HttpContext.Current.Request.Url.Authority;
  else
    return "http://" + HttpContext.Current.Request.Url.Authority +            HttpContext.Current.Request.ApplicationPath;
}

string URL = GetWebAppRoot() + "/URL.aspx?";

2) CREATE ENCRYPTION CLASS

public class QueryStringEncryption
{
    private byte[] key = { };
    private byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
     
    public string Decrypt(string stringToDecrypt)
    {
        byte[] inputByteArray = new byte[stringToDecrypt.Length + 1];
        try
        {
           key = System.Text.Encoding.UTF8.GetBytes("mkadm11n");
           DESCryptoServiceProvider des = new DESCryptoServiceProvider();
           inputByteArray = Convert.FromBase64String(stringToDecrypt);
           MemoryStream ms = new MemoryStream();
           CryptoStream cs = new CryptoStream(ms,
           des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
           cs.Write(inputByteArray, 0, inputByteArray.Length);
           cs.FlushFinalBlock();
           System.Text.Encoding encoding = System.Text.Encoding.UTF8;
           return encoding.GetString(ms.ToArray());
         }
         catch (Exception e)
         {
                return e.Message;
         }
     }

     public string Encrypt(string stringToEncrypt)
     {
        try
        {
           key = System.Text.Encoding.UTF8.GetBytes("mkadm11n");
           DESCryptoServiceProvider des = new DESCryptoServiceProvider();
           byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
           MemoryStream ms = new MemoryStream();
           CryptoStream cs = new CryptoStream(ms,
           des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
           cs.Write(inputByteArray, 0, inputByteArray.Length);
           cs.FlushFinalBlock();
           return Convert.ToBase64String(ms.ToArray());
        }
        catch (Exception e)
        {
                return e.Message;
        }
 }

2)  ENCRYPT INPUT PARAM

1)URLEncode param 1st, because special word like ?,= will have error
2)Encypt it to protect the URL, not let user to see the info

QueryStringEncryption queryStringEncrption = new QueryStringEncryption();

string queryString = string.Format("UserID={0}&NewPassword={1}&SentDate={2}", user.UserID, HttpUtility.UrlEncode(user.Password), HttpUtility.UrlEncode((DateTime.Now).ToString()));
string encyptedQS = queryStringEncrption.Encrypt(queryString);

URL = URL + encyptedQS;
content = content + "Please Click this <a href=\"" + URL + "\">link </a>";

3)  URL PAGE LOAD

1)Get Raw URL
2)Decrypt and URLDecode

queryString = Request.RawUrl;
queryString = queryString.Substring(queryString.IndexOf('?') + 1);

queryString = queryStringEncrption.Decrypt(queryString);

string[] arrMsgs = queryString.Split('&');
string[] arrValue;
                      
arrValue = arrMsgs[0].Split('='); //Get Name

long userID = Convert.ToInt64(HttpUtility.UrlDecode(arrValue[1].ToString()));

No comments:

Post a Comment