Sunday, August 19, 2012

Encrypt and Decrypt Connection Strings in Web.Config

Encryption and Decryption using Web.Config

File->New->WebSite



Go to web.config file and write the following code.








<connectionStrings>
    <add name="dbemployee" connectionString="Data Source=localhost; user id=sa;password=uday; database=uday"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

Now go to Visual Stdio CommandPrompt and type the following to encypt the web.config file.

aspnet_regiis.exe -pef "connectionStrings" path of the file

Eg: aspnet_regiis.exe -pef "connectionStrings"  C:\Users\uday\Documents\WebSite3














here pef indicates file system website.

Now go to web.config and see the web.config file with the encrypted code

Web.config






















Decrypt the Connection String

Now go to Visual Stdio CommandPrompt and type the following to decrypt the web.config file

aspnet_regiis.exe -pdf "connectionStrings" path of the file

Eg: aspnet_regiis.exe -pdf "connectionStrings"  C:\Users\uday\Documents\WebSite3














Web.config




Working with Cookies

Working with Cookies

Cookies : The information which is stored in Client Machine.

Syntax for creating the cookie
To create or retrieve information from cookie we have a class called as
1)  HttpCookie object=new HttpCookie(cookiename, cookievalue);
Now to store the above cookie we have to write the next line asResponse.Cookies.Add(object);

2) HttpCookie object=new HttpCookie(cookiename);object.Value=cookie.value;Response.Cookies.Add(object);

Retrieving the information from a Cookie
Syntax1)  HttpCookie object=Request.Cookies["Cookiename"];variable=object.Value;
2) variable=Request.Cookies["Cookiename"].value;

Cookies are classified into Two Types
1 In Memory Cookies2 Out Memory Cookies

In Memory Cookies: Are again classified into two types1 Single Value Cookie2 Multi Value Cookie
Out Memory Cookies:

Are again classified into two types1 Single Value Cookie2 Multi Value Cookie
InMemory Cookie:The cookies which are stored in the browser process memory are called as inmemory cookie.

These cookies will automatically destroy themselves when the browser is closed.so the default life span of the inmemory cookie is till the browser is on.

SingleValueCookie

Which stores only the single cookie is called as single value cookie.

Example
Default.aspx


Default.aspx.cs


Multi Value Cookie 
 A cookie which holds multiple values in a single name.

How to Create MultiValue Cookie

HttpCookie object=new HttpCookie(cookiename);
object.Values["key"]="value";
or
object.Values.Add("key","value1");
object.Values.Add("key","value2");

To Store the Cookie 
Response.Cookies.Add(object);

To Retrieve information

HttpCookie object =Request.Cookies["CookieName"];

To get Count
Object.Values.Count

To get Keys
Object.Values.GetKey(index);

To get KeyValues
Object.Values[index];
Object.Values.Get(index);

Example 
Default.aspx


















Default.aspx.cs





Output


When Click on Create Cookie  

It displays a Message as Cookie Created.




When Click on Display Cookie



Out Memory Cookie

A cookie which is stored permanently on the hard disc of a client machine .

Syntax for Creating  Single Value Cookie

HttpCookie obj=new HttpCookie("cookiename");
obj.Value=someValue;
obj.Expires=DateTime.Now.AddMinutes();

Supported Methods

AddSeconds();
AddHours();
AddMonths();
AddYears();
AddDays();
DateTime.MaxValue

To Store Cookie in the Hard Disc

Response.Cookies.Add(object);

Default.aspx

       <table>
       <tr>
       <td>
           <asp:Label ID="Label1" runat="server" Text="UserName"></asp:Label>
       </td>
       <td>
           <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
       </td>
       </tr>
        <tr>
       <td>
           <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
       </td>
       <td>
           <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
       </td>
       </tr>
       <tr>
       <td colspan="2">
           <asp:CheckBox ID="CheckBox1" runat="server" Text="Remember UserName & Password"/>
       </td>
       </tr>
       <tr>
       <td colspan="2">
             <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
       </td>
       </tr>
       </table>



Default.aspx.cs


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["username"] != null && Request.Cookies["password"] != null)
        {
            if (Request.Cookies["username"].Value.ToString() == "uday" && Request.Cookies["password"].Value.ToString() == "uday")
            {

                Response.Redirect("Default2.aspx");
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (txtUserName.Text == "uday" && txtPassword.Text == "uday")
        {
            if (CheckBox1.Checked == true)
            {
                HttpCookie cookie = new HttpCookie("username", txtUserName.Text);
                cookie.Expires = DateTime.MaxValue;
                HttpCookie p = new HttpCookie("password", txtPassword.Text);
                p.Expires = DateTime.MaxValue;
                Response.Cookies.Add(cookie);
                Response.Cookies.Add(p);
            }
        }
    }
}

Output







________________________________________________________________




________________________________________________________________



Example for writing the cookies

Response.Cookies["uday"].Value = "abc";
Response.Cookies["uday"].Expires = DateTime.Now.AddDays(1);
_________________________________________________________________________________

HttpCookie anvesh= new HttpCookie("goud");
anvesh.Value = DateTime.Now.ToString();
anvesh.Expires = DateTime.Now.AddDays(1);Response.Cookies.Add(anvesh);
__________________________________________________________

Reading Cookies

if(Request.Cookies["uday"] != null)
    lblCookie.Text = Server.HtmlEncode(Request.Cookies["uday"].Value);
__________________________________________________________

Deleting Cookies
HttpCookie demoCookie = new HttpCookie("uday");demoCookie.Value = DateTime.Now.ToString();demoCookie.Expires = DateTime.Now.AddDays(-1);Response.Cookies.Add(uday);
________________________________________________________________

Advantages

1  Cookies are stored on client.
2  Easy to Implement
3. You can configure cookies to expire when the browser session ends (session cookies) or they can exist for a specified length of time on the client computer (persistent cookies). 

Disadvantages

1 User can delete cookie

2 No Security



Wednesday, August 15, 2012

GridView using ArrayList

ArrayList

It is the set of Dissimilar data types.

It is under System.Collections NameSpace


Default.aspx


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>



Default.aspx.cs


using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page

{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Data();
        }
    }
    private void Data()
    {
        string[] arrlist = { "uday","raju","anvesh","kittu","sandhya" };
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        for (int i = 0; i < arrlist.Count(); i++)
        {
            dt.Rows.Add();
            dt.Rows[i]["Name"] = arrlist[i].ToString();
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}


Output

Wednesday, August 8, 2012

DataList Control

It is similar to repeater Control.

It provides editing  Facilities.

It provides adding new Records



It does not Provide Sorting  and Paging Facilities.















Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
         <asp:DataList ID="DataList1" runat="server">
        <HeaderTemplate>
        <tr>
        <th>
        EmpNo
        </th>
        <th>
        EmpName
        </th>
        <th>
        Designation
        </th>
        </tr>
        </HeaderTemplate>
        <ItemTemplate>
        <tr>
        <td> <%#DataBinder.Eval(Container.DataItem,"eno")%></td>
        <td> <%#DataBinder.Eval(Container.DataItem,"ename")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"designation")%></td>
        </tr>
        </ItemTemplate>
     
        </asp:DataList>
    </table>
    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


public partial class RepeaterControl : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        SqlCommand cmd = new SqlCommand("select *from employee", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataList1.DataSource = ds;
        DataList1.DataBind();
    }
}









Working with Repeater Control

Repeater Control

This Control is used to display the data in plain Text.

Repeater Control does not provide designing Facilities, Editing Facilities, Sorting Facilities

It is faster as compared to GridView, DataList

It is just used to display the data in plane format.














Default.aspx


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
        <tr>
        <th>
        EmpNo
        </th>
        <th>
        EmpName
        </th>
        <th>
        Designation
        </th>
        </tr>
        </HeaderTemplate>
        <ItemTemplate>
        <tr>
        <td> <%#DataBinder.Eval(Container.DataItem,"eno")%></td>
        <td> <%#DataBinder.Eval(Container.DataItem,"ename")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"designation")%></td>
        </tr>
        </ItemTemplate>
        </asp:Repeater>
    </table>
    </div>
    </form>
</body>
</html>



Default.aspx.cs


using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


public partial class RepeaterControl : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        SqlCommand cmd = new SqlCommand("select *from employee", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        Repeater1.DataSource = ds;
        Repeater1.DataBind();
    }
}





Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...