0% found this document useful (0 votes)
9 views

ASP Notes

The document discusses different controls and configuration in ASP.Net including GridView, Repeater, DataList, Machine.config, Web.config, application settings, and connection strings. It also covers security concepts of authentication and authorization.

Uploaded by

u3552703
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

ASP Notes

The document discusses different controls and configuration in ASP.Net including GridView, Repeater, DataList, Machine.config, Web.config, application settings, and connection strings. It also covers security concepts of authentication and authorization.

Uploaded by

u3552703
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

19/02/2024 Paging in GridView in ASP.

Net
Right Click -> Properties -> Allow Paging (true) ->Page Size(enter preferred
pages) -> Go to Event -> Page Index Changing (handle this event) ->

Code - GridView1.PageIndex = e.NewPageIndex;


Button1_Click(sender,e);

Repeater Control
Repeater is a unstructured control. We have design it's column by our requirement.

DataList
This control is used to display back-end data in front-end like GridView.
The Speciality of this control is that it display the data in the form of columns.
It is not able to show data itself if we want to show data we have to design it's column.
20/02/2024 Configuration Support in ASP.Net

Machine.config file Web.config file


-global files & same *local file different for
for each project each project
*It consist xml files
*We can perform 3
settings in this file
1- Application Settings
2- Security Settings
3- Mail related Settings

Note- In HTML there are predefined tags while in XML we can define our own tags. XML file is not compile direct run

Application Related Settings


1- <appsetting> tag - This tag is used to define static wide global variables. We can use these
variables anywhere in our project.
Required namespace - System.Configuration
Required class - ConfigurationManager
key=variableName, value=variable value

Code- ----Web.config file----

<appsettings>
<add key="ri" value="18.24"/>
</appsettings>

----C# file----
using System.Configuration
Lable1.Text=ConfigurationManager.Appsettings["ri"];

2- <connectionstring> tag - This tag is used to define connection string globaly by which we can call it anywhere in our
project

Code- ----Web.config file----

<connectionstring>
<add name="sqlcn" connectionstring="___________Connection string"/>
</connectionstring>

----C# file----
using System.Configuration
string c = ConfigurationManager.ConnectionStrings["sqlcn"].ConnectionString; (define it globally)

22/02/2024 Security in ASP.Net

Operating System provides security to their resources by using authentication & authorization. These two
concepts are used in ASP.Net to implement security.

Authentication
The process of verifieng user credential against specified data source is called as authentication.

Authorization
The process of allocating proper resourcesis performed by authorization.
Authentication has 3 types:
1. Windows Authentication- Used for Intranet Application
2. Passport Authentication
3. Forms Authentication- Used for Internet Application
Windows Authentication:
Client Request to access IIS Server WAT (Windows Authentication Ticket
view secured pages
verify

login DB

Passport Authentication:

Request to access
Verify
IIS server
User View pages by PAT
fir y
ev ot
ytir

Log
in W
oh

ind
tua

ow

Match/authenticate
Third Party
provide PAT(Passport
Authentication Ticket DB

23/02/2024 Form Authentication:

IIS
Server
Request to access
secured pages

view secured pages by FAT ASP.Net


verify/send login window Application
User Ch
e
au ck &
the
nti gene
ca
tio rate f
nt o
ick rm
et(
FA
T)

DB

How to impelment Security in web forms


Create a project -> create a folder-> add secured web pages in folders-> craete a login page in root directory -> in external
web.config file -> Follow below process----
<system.web>
<authentication mode="Forms">
</authentication>
</system.web>

add a new web.config file in your folder ->


------follow below process-------
<system.web>
<authorization>
<deny users ="?"/>
</authorization>
</systwm.web>

-----Write below code in login button------


using System.Web.Security;
if (TextBox1.Text =="ram" && TextBox2.Text =="seeta")
{
FormsAuthentication.RedirectFromLoginPage(TextBox1.Text,CheckBox1.Checked);
Response.Redirect("~/admin/admin_home.aspx");
}
else
{
Response.Write("invalid user name and password..");
}
24/02/2024 Form Authentication class

This class is responsible to create ticket.


This class is present in System.Web.Security namespace.
There are two types of tickets..
1- In memory Ticket - This ticket is destroyed when browser is closed.
2- Persistent Ticket- This ticket is destroyed when browser history & cookies is deleted. This ticket
is destroyed after 20 minutes.
To set our login page with different name....
Go to external web.config file -> follow below steps inside the <authentication mode> tag
<forms loginUrl="login1.aspx">
</forms>

27/02/2024 How to upload/store images/media files in ASP.Net

There are two ways to store image in Database:


1- Directly store image in Database.
2- We can store image path in Database.
Code:
if (FileUpload1.HasFile)
{
if (FileUpload1.HasFile)
{
string strname = FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/img/") + strname);
string p = "~/img/" + strname;
string q = "insert into tbl_img values('" + TextBox1.Text + "','" + p + "')";
SqlConnection cn = new SqlConnection("Data Source=DESKTOP;Initial Catalog=MyProject;Integrated
Security=True;Encrypt=False");
cn.Open();
SqlCommand cmd = new SqlCommand(q, cn);
int i = cmd.ExecuteNonQuery();
if (i > 0)
Response.Write("saveddddddd");
}
}

28/02/2024 Auto Generate Password

Code: using System;


using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

}
public string CreatePassword(int length)
{
const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder res = new StringBuilder();
Random rnd = new Random();
while (0 < length--)
{
res.Append(valid[rnd.Next(valid.Length)]);
}
return res.ToString();
}

protected void Button1_Click(object sender, EventArgs e)


{
string pwd = CreatePassword(5);
string s = "insert into Emp_Reg values('" + TextBox1.Text + "','" + pwd + "')";
SqlConnection cn = new SqlConnection("Data Source=DESKTOP;Initial Catalog=MyProject;Integrated
Security=True;Encrypt=False");
cn.Open();
SqlCommand cmd = new SqlCommand(s, cn);
cmd.ExecuteNonQuery();
Response.Write("inserteddddd");

}
}
}
How to fetch Image from Database / server in ASP.Net

_______________________________

29/02/2024 Cookies in ASP

With the help of cookies We can generate Ticket to access secured pages by verifying identity.
To generate cookies in ASP.Net we use HttpCookie class which is present in System namespace.

HttpCookie => 1. It is a class


2. It is name=value pair like a variable which reveals between every request and response.

There are two types of cookies:


i- in memory
ii- persistance
In Memory cookies: It is stored in browser till the browser is opened. When the browser closed it will be destroyed
It is stored at client side

Persistance cookies: It is stored in browser till its set timing. When the timing end it will be destroyed.

Note- Cookies are maintain and stored at client side

Login Page Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;
using System.Configuration;

namespace Secured_Application
{
public partial class Login_Page : System.Web.UI.Page
{
string c = ConfigurationManager.ConnectionStrings["sqlcn"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
SqlConnection cn = new SqlConnection(c);
cn.Open();
string q = "select Id from reg where Name='" + TextBox1.Text + "' AND Password='" + TextBox2.Text + "'";
SqlCommand cmd = new SqlCommand(q, cn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
HttpCookie ck = new HttpCookie("mycookie");
ck.Value = TextBox1.Text;
if (CheckBox1.Checked)
{
ck.Expires = DateTime.Now.AddHours(1);
}
Response.Cookies.Add(ck);
Response.Redirect("~/Userhomepage/Userhome.aspx");
}
else
{
Response.Write("Invalid User");
}

}
}
}
Secured page code

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

namespace Secured_Application.Userhomepage
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["mycookie"]!=null)
{
Label1.Text = Request.Cookies["mycookie"].Value;
}
else
{
Response.Redirect("~/login.aspx");
}
}
}
}

01/03/2024
How to Destroy cookies in ASP.Net

protected void LinkButton1_Click(object sender, EventArgs e)


{
Response.Cookies["mycookie"].Expires = DateTime.Now.AddDays(-1);
Response.Redirect("~/login.aspx");
}
We can send value to multiple pages by using cookies.

DrawBack Of cookies

1- Less Secure
2- It uses client storage
3- Large amount of data we can not store in Cookies

To overcome these problems we use session Object.................

Session in ASP.Net
1--
protected void Button1_Click(object sender, EventArgs e)
{
if(.............)
Session["mycookie"]=TextBox1.Text;
Response.Redirect("~/user/user_home.aspx");
}
2--
if(Session["mycookie"]!=null)
{
Label1.Text=Session["mycookie"].ToString
}

Secured Page Code:


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

namespace Secured_Application.Userhomepage
{
public partial class Userhome : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["mycookie"] != null)
{
Label1.Text = Session["mycookie"].ToString();
Session.Timeout = 1; //minutes.. 20 min
}
else
{
Response.Redirect("~/login.aspx");
}

protected void LinkButton1_Click(object sender, EventArgs e)


{
Response.Cookies["mycookie"].Expires = DateTime.Now.AddDays(-1);
Response.Redirect("~/login.aspx");

protected void Button1_Click(object sender, EventArgs e)


{
Response.Redirect("~/Userhomepages/Userhome1.aspx");

}
}
}

Login Page Code:


using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Secured_Application
{
public partial class Login : System.Web.UI.Page
{
//string c = ConfigurationManager.ConnectionStrings["sqlcn"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
SqlConnection cn = new SqlConnection("Data Source=DESKTOP;Initial Catalog=MyProject;Integrated
Security=True;Encrypt=False");
cn.Open();
string q = "select Id from reg where Name='" + TextBox1.Text + "' AND Password='" + TextBox2.Text + "'";
SqlCommand cmd = new SqlCommand(q, cn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["mycookie"] = TextBox1.Text;

Response.Redirect("~/Userhomepage/userhome.aspx");
}
else
{
Response.Write("Invalid User");

}
}
}
}

05/03/2024 How to destroy Session in ASP.Net

Session.RemoveAll(); -> It will destroy all sessions.

09/03/2024 Stored Procedure

Stored Procedures are the precompiled block of code.


It is the part of Back-end (Database)

You might also like