Seach

Showing posts with label asp.net. Show all posts
Showing posts with label asp.net. Show all posts

how to encrypt and decrypt a quire string c# asp.net

sender page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;//used for streams support
using System.Security.Cryptography;//used for security
using System.Text;//used for encoding
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    public static string Encrypt_QueryString(string str)
    {
        string EncrptKey = "god you are great";
            //"2013;[pnuLIT)WebCodeExpert";
        byte[] byKey = { };
        byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
        byKey = System.Text.Encoding.UTF8.GetBytes(EncrptKey.Substring(0, 8));
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        byte[] inputByteArray = Encoding.UTF8.GetBytes(str);
        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());
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string val = Encrypt_QueryString(TextBox1.Text.Trim());
        Response.Redirect("Default2.aspx?EmpId=" + val);
    }
  }
redirecting page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.IO;
using System.Text;

public partial class Default2 : System.Web.UI.Page
{
    public static string Decrypt_QueryString(string str)
    {
        str = str.Replace(" ", "+");
        string DecryptKey = "god you are great";
            //"2013;[pnuLIT)WebCodeExpert";
        byte[] byKey = { };
        byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
        byte[] inputByteArray = new byte[str.Length];

        byKey = System.Text.Encoding.UTF8.GetBytes(DecryptKey.Substring(0, 8));
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        inputByteArray = Convert.FromBase64String(str);
        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());
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string Id = Decrypt_QueryString(Request.QueryString["EmpId"]);
        Response.Write("Emp Id =:" + Id);
    }
}

how to findout ip address in c# asp.net application

 String ip = System.Net.Dns.GetHostName();
        string myIP = System.Net.Dns.GetHostByName(ip).AddressList[0].ToString();

how to send email in c# asp.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SmtpClient sc = new SmtpClient("smtp.gmail.com", 587)
        {
            Credentials =new NetworkCredential("your mailid","your password"),EnableSsl=true       

        };
        sc.Send("from mail", "to mail", "subject", "body");
    }
}

How to Upload and display image and show full path of image

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    //System.String name, ext;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName.ToString());

        if (FileUpload1.HasFile)
        {
            if (fileExtension == ".jpg" || fileExtension == ".png")
            {
                string a = Server.MapPath("~/image/");
                FileUpload1.SaveAs(a + FileUpload1.FileName);


                Image1.ImageUrl = "~/image/" + FileUpload1.FileName;
                Label1.Text = FileUpload1.FileName;
                Label3.Text="~/image/" + FileUpload1.FileName;


            }

            else
            {

                Label2.Text = "invalid file extension";





             //   Label1.Text = "pls upload a file";


            }





        }

    }
}

HOW TO PROVIDE A CUSTOM LOGIN DEMO USING SQLSERVER WITH C#-ASP.NET

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; //public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { DropDownList1.SelectedItem.Text.ToLower();//TO GET THE SELECTED ITEM IN LOWERCASE if ((DropDownList1.SelectedItem.Text != "") && (TextBox2.Text != "")) { string ss = ConfigurationManager.ConnectionStrings["con"].ToString(); SqlConnection c = new SqlConnection(ss); try { c.Open(); SqlCommand cmd = new SqlCommand("select statement " ",c); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read())//FOR FETCHING EACH RECODE { if ((DropDownList1.SelectedItem.Text == "admin") && (TextBox2.Text == "admin")) { string s = DropDownList1.SelectedItem.Text.ToString(); //Server.Transfer("defalut3.aspx?name='"+s+"'"); Server.Transfer("abcd.aspx?name='"+s+"'"); } else if ((DropDownList1.SelectedItem.Text == dr[0].ToString()) && (TextBox2.Text == dr[1].ToString())) { string s1 = DropDownList1.SelectedItem.Text; Server.Transfer("default2.aspx?a='" + s1 + "'"); } else { //string jScript; //jScript = //""; // Page.RegisterClientScriptBlock("warning", jScript);//TO ENABLE CLIENT SIDE SCRIPTING IN CODE BEHIND FILE } } } finally { c.Close(); } } } }

How to provide a javascript alert in .cs file in c#-asp.net

//string jScript = //""; // Page.RegisterClientScriptBlock("warning", jScript);